How should logging be configured in a library vs an application?
IntermediateConceptExceptions, context managers, files & logging
Answer
Libraries and modules only create loggers: log = logging.getLogger(__name__). They never call basicConfig or add handlers, so the application stays in control.
The application entry point configures logging once:
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"),
format="%(asctime)s %(levelname)s %(name)s: %(message)s")
logging.getLogger("botocore").setLevel(logging.WARNING)
Calling basicConfig in an imported module is why "my log level change does nothing": only the first basicConfig call has effect (unless force=True).
More on Exceptions, context managers, files & logging
How does `try` / `except` / `else` / `finally` work?BeginnerWhy is a bare `except:` (or `except Exception: pass`) dangerous?BeginnerWhat is exception chaining, and what is `raise ... from`?IntermediateWhen should you define custom exception classes?IntermediateWhat is the EAFP vs LBYL style, and which does Python prefer?IntermediateHow does a `with` statement work, and why use it for files?Beginner