Skip to content

Local Logging Configuration Overwritten by Package's __init__.py #118

@Felix-Nienaber

Description

@Felix-Nienaber

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

  1. Our application imports brickschema
  2. 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,
)
  1. 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)
  1. 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 basicConfig after 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions