[Python] Setup logging before importing other modules

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    What is the standard way for setting up the logging system (formatting, level), before modules are imported?

    The reason I want to do this is that during the import phase complex data structures are loaded, and progress is shown with loggers. But since the logging level is not set, I do not even see those log entries (level is by default WARNING)

    I could do a hack like this:

    import logging

    logging.basicConfig(...)

    import mymodule


    But I do not like running code before importing stuff.

    Isn't there an accepted way to "pre-configure" the logging system from outside the application, with env variables or any such alternative? Something like:

    LOG_LEVEL=INFO LOG_FORMAT="..." python main.py


    I can of course evaluate those env vars myself (and at that point my hack above is good enough), but I would like to reuse existing ideas before implementing my own solution.

    Continue reading...

Compartilhe esta Página