Advertisement
Beginner Python, Logging

What does logging.basicConfig() do, and what are its common pitfalls?

Short answer

basicConfig() configures the root logger with a default handler, formatter, and level in one call -- convenient for quick scripts.

Save Python mock interview
Advertisement
AModel answer
basicConfig() configures the root logger with a default handler, formatter, and level in one call -- convenient for quick scripts. Its main pitfall is that it only has an effect the first time it's called (subsequent calls are ignored unless force=True), which confuses developers who call it in multiple places expecting it to reconfigure logging each time.
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(message)s')
logging.debug('This will show with the configured format')
Real-world example
A script's second basicConfig() call with a different level silently has no effect because logging was already configured elsewhere, causing confusing debugging sessions.
Likely follow-ups
  • How do you force reconfiguration with force=True?
  • Why shouldn't libraries call basicConfig at import time?
Related topics