[Python] Logging Inheritance in Python

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I am currently developing a core utils package where I want to set some logging properties (I know that this is not best practice, but it´s for interal purposes and intended to generate logs). When I now import the package nothing gets logged:

    # core.__main__.py

    class BaseLoggerConfig(BaseModel):

    LOG_FORMAT: str = "%(levelprefix)s %(asctime)s %(name)s:%(lineno)d: %(message)s"
    DATEFMT: str = "%Y-%m-%d %H:%M:%S"

    LOG_LEVEL: int = logging.INFO

    version: int = 1
    disable_existing_loggers: bool = False
    formatters: dict = {
    "default": {
    # "()": "uvicorn.logging.DefaultFormatter",
    "fmt": LOG_FORMAT,
    "datefmt": DATEFMT,
    },
    }
    filters: dict = {}

    handlers: dict = {
    "default": {
    "formatter": "default",
    "class": "logging.StreamHandler",
    "stream": "ext://sys.stderr",
    }
    }

    loggers: dict = {}

    def __init__(self, name: str, **data):
    super().__init__(**data)

    self.loggers[name] = {
    "handlers": ["default"],
    "level": self.LOG_LEVEL,
    "propagate": False,
    }

    LOG_CONFIG = BaseLoggerConfig(__name__)

    logging.config.dictConfig(LOG_CONFIG)




    - core.__main__
    Level: INFO
    Handlers: ['StreamHandler']



    I now have logging in my other files, like:

    # core.utils
    import logging

    logger = logging.getLogger(__name__)

    def test():
    logger.info(f"I am a log from {__name__}")



    # test.py
    import logging
    from core.utils import test

    logger = logging.getLogger(__name__)

    test()




    What am I missing?

    Continue reading...

Compartilhe esta Página