-
Notifications
You must be signed in to change notification settings - Fork 19
Open
Description
Issue Description
The brickschema package overwrites global logging configuration in its __init__.py file by calling logging.basicConfig(). This causes unexpected behavior where custom logging configurations in consuming applications are lost.
Current Behavior
- Our application imports
brickschema - Upon importing brickschema, the package's init.py calls:
logging.basicConfig(
format="%(asctime)s,%(msecs)03d %(levelname)-7s [%(filename)s:%(lineno)d] %(message)s",
datefmt="%Y-%m-%d:%H:%M:%S",
level=logging.WARNING,
)- Our application tries to set up the logging to use a
RotatingFileHandler
rotating_loghandler = handlers.RotatingFileHandler(log_filename, maxBytes=1024 * 1024 * 2, backupCount=1)
logging.basicConfig(handlers=[rotating_loghandler], format="%(asctime)s %(name)s %(levelname)s %(message)s", level=loglevel)- Our logs are no longer saved to the file.
Why This Happens
basicConfig()first checks if the root logger already has any handlers- If handlers exist, it silently returns without doing anything
- This is a safety feature to prevent accidental reconfiguration
- However, the silent failure makes debugging difficult, epecially because any user would normally only set their
basicConfigafter importing all packages
Why This Is Problematic
logging.basicConfig()is meant for basic application-level configuration- Libraries should not modify the root logger or call
basicConfig() - This violates the principle of least surprise and makes debugging difficult
- Applications lose control over their logging setup
Workaround
The workaround for us was simple, we just call basicConfig with the parameter force=True:
logging.basicConfig(handlers=[rotating_loghandler], format="%(asctime)s %(name)s %(levelname)s %(message)s", level=loglevel, force=True)This solved the issue for our implementation, but as this is not expected behavior and happens silently, we would still recommend this to be remedied
Metadata
Metadata
Assignees
Labels
No labels