How python logging get it's configuration

Go To


I used to Python logging, it works fine. The logging.basicConfig(...) set in one module (a file), then we can use logging every where. Obviously, logging is global.

The question is how logging find it's settings, when we not call the module where basicConfig(...) appeared (in file )? Is logging scan all the packages?

Even the logging.basicConfig(...) put into an and the module ( never get imported, or not used anywhere, the logging setting take effect!

2012-04-04 06:34
by Andrew_1510


To understand logging you have dive into Python's standard library sources.

Here is the trick:

root = RootLogger(WARNING)
Logger.root = root
Logger.manager = Manager(Logger.root)

# and 
def basicConfig(**kwargs):
        hdlr = StreamHandler(stream)
        fs = kwargs.get("format", BASIC_FORMAT)
        dfs = kwargs.get("datefmt", None)
        style = kwargs.get("style", '%')
        fmt = Formatter(fs, dfs, style)

So, when you call basicconfig() with certain parameters, root logger is set. Finally getLogger:

def getLogger(name=None):
    Return a logger with the specified name, creating it if necessary.

    If no name is specified, return the root logger.
    if name:
        return Logger.manager.getLogger(name)
        return root
2012-04-04 07:46
by Zaur Nasibov
+1 for clear code samples. Still wondering when basicConfig get called - Andrew_1510 2012-04-04 08:00


I think there is no magic scanning here.

  • Try to test it this way in a separate test directory:

        import logging'test')

    test/ import logging logging.basicConfig(filename='test.log', level=logging.INFO)


    Result: NO test.log file.

  • Now let's update the test:

        import logging
        import any'test')


    Result: new test.log file with INFO:root:test string inside.

  • So I guess that in your case is imported somehow, despite your expectations.

    You may find the way is imported easily, just add few lines there:

        from traceback import print_stack



    File "", line 2, in 
        import any
    File "", line 2, in 

    This stack shows that is imported from

    I hope you will find where it is imported from in your case.

2012-04-05 05:25
by Denis Ryzhkov
Yes, no magics happens when it put into a new place. In my old project, I do put the logging setting logging.basicConfig() into a new and not-used file, and it do show up. Only thing possible to scan the file is the testing tool, pytest in case - Andrew_1510 2012-04-06 17:54