Skip to content

Remove global logging configuration from library code #29

@iladrien-hub

Description

@iladrien-hub

The library currently calls logging.basicConfig() which modifies global logging configuration. This is bad practice for libraries as it interferes with application-level logging setup

import logging
import logging.handlers
import os
import sys

import lazy_import


def _get_log_handlers():
    if fn := os.getenv("LOG_FILE"):
        return [logging.handlers.RotatingFileHandler(fn, maxBytes=512 * 2 ** 20, backupCount=5)]
    else:
        return [logging.StreamHandler(sys.stdout)]

logging.basicConfig(
    format="%(asctime)s | %(levelname)7s | %(name)16s | %(module)s.%(funcName)s.%(lineno)d: %(message)s",
    level=logging.getLevelName(logging.INFO),
    handlers=_get_log_handlers()
)

logging.info("Hello, world!")

Expectation:

2025-05-29 13:49:11,141 |    INFO |             root | 2.<module>.21: Hello, world!

Reality:

INFO:root:Hello, world!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions