Understanding Python's logging module
Sunday, August 6th, 2017
I'm slightly embarrassed to say that after almost two decades of programming Python, I still didn't understand its logging module. Sure, I could get it to work, and reasonably well, but I'd often end up with unexplained situations such as double log lines or logging that I didn't want.
>>> requests.get('https://www.electricmonk.nl') DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): www.electricmonk.nl DEBUG:requests.packages.urllib3.connectionpool:https://www.electricmonk.nl:443 "GET / HTTP/1.1" 301 178
What? I never asked for that debugging information?
So I decided to finally, you know, read the documentation and experiment a bit to figure out how the logging module really works. In this article I want to bring attention to some of the misconceptions I had about the logging module. I'm going to assume you have a basic understanding of how it works and know about loggers, log levels and handlers.
Loggers have a hierarchy. That is, you can create individual loggers and each logger has a parent. At the top of the hierarchy is the root logger. For instance, we could have the following loggers:
myapp myapp.ui myapp.ui.edit
These can be created by asking a parent logger for a new child logger:
>>> log_myapp = logging.getLogger("myapp") >>> log_myapp_ui = log_myapp.getChild("ui") >>> log_myapp_ui.name 'myapp.ui' >>> log_myapp_ui.parent.name 'myapp'
Or you can use dot notation:
>>> log_myapp_ui = logging.getLogger("myapp.ui") >>> log_myapp_ui.parent.name 'myapp'
You should use the dot notation generally.
One thing that's not immediately clear is that the logger names don't include the root logger. In actuality, the logger hierarchy looks like this:
More on the root logger in a bit.
Log levels and message propagation
Each logger can have a log level. When you send a message to a logger, you specify the log level of the message. If the level matches, the message is then propagated up the hierarchy of loggers. One of the biggest misconceptions I had was that I thought each logger checked the level of the message and if it the level of the message is lower or equal, the logger's handler would be invoked. This is not true!
What happens instead is that the level of the message is only checked by the logger you give the message to. If the message's level is lower or equal to the logger's, the message is propagated up the hierarchy, but none of the other loggers will check the level! They'll simply invoke their handlers.
>>> log_myapp.setLevel(logging.ERROR) >>> log_myapp_ui.setLevel(logging.DEBUG) >>> log_myapp_ui.debug('test') DEBUG:myapp.ui:test
In the example above, the root logger has a handler that prints the message. Even though the "log_myapp" handler has a level of
DEBUG message is still propagated to to the root logger. This image (found on this page) shows why:
As you can see, when giving a message to a logger, the logger checks the level. After that, the level on the loggers is no longer checked and all handlers in the entire chain are invoked, regardless of level. Note that you can set levels on handlers as well. This is useful if you want to, for example, create a debugging log file but only show warnings and errors on the console.
It's also worth noting that by default, loggers have a level of 0. This means they use the log level of the first parent logger that has an actual level set. This is determined at message-time, not when the logger is created.
The root logger
The logging tutorial for Python explains that to configure logging, you can use
It's not immediately obvious, but what this does is configure the root logger. Doing this may cause some counter-intuitive behaviour, because it causes debugging output for all loggers in your program, including every library that uses logging. This is why the requests module suddenly starts outputting debug information when you configure the root logger.
In general, your program or library shouldn't log directly against the root logger. Instead configure a specific "main" logger for your program and put all the other loggers under that logger. This way, you can toggle logging for your specific program on and off by setting the level of the main logger. If you're still interested in debugging information for all the libraries you're using, feel free to configure the root logger. There is no convenient method such as
basicConfig() to configure a main logger, so you'll have to do it manually:
ch = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s %(levelname)8s %(name)s | %(message)s') ch.setFormatter(formatter) logger = logging.getLogger('myapp') logger.addHandler(ch) logger.setLevel(logging.WARNING) # This toggles all the logging in your app
There are more pitfalls when it comes to the root logger. If you call any of the module-level logging methods, the root logger is automatically configured in the background for you. This goes completely against Python's "explicit is better than implicit" rule:
#!/usr/bin/env python import logging logging.warn("uhoh") # Output: WARNING:root:uhoh
In the example above, I never configured a handler. It was done automatically. And on the root handler no less. This will cause all kinds of logging output from libraries you might not want. So don't use the
logging.error() and other module-level methods. Always log against a specific logger instance you got with
This has tripped me up many times, because I'll often do some simple logging in the main part of my program with these. It becomes especially confusing when you do something like this:
#!/usr/bin/python import logging for x in foo: try: something() except ValueError as err: logging.exception(err) pass # we don't care
Now there will be no logging until an error occurs, and then suddenly the root logger is configured and subsequent iterations of the loop may start logging messages.
The Python documentation also mentions the following caveat about using module-level loggers:
The above module-level convenience functions, which delegate to the root logger, call
basicConfig()to ensure that at least one handler is available. Because of this, they shouldnot be used in threads, in versions of Python earlier than 2.7.1 and 3.2, unless at least one handler has been added to the root logger before the threads are started. In earlier versions of Python, due to a thread safety shortcoming in
basicConfig(), this can (under rare circumstances) lead to handlers being added multiple times to the root logger, which can in turn lead to multiple messages for the same event.
Debugging logging problems
When I run into weird logging problems such as no output, or double lines, I generally put the following debugging code at the point where I'm logging the message.
log_to_debug = logging.getLogger("myapp.ui.edit") while log_to_debug is not None: print "level: %s, name: %s, handlers: %s" % (log_to_debug.level, log_to_debug.name, log_to_debug.handlers) log_to_debug = log_to_debug.parent
level: 0, name: myapp.ui.edit, handlers:  level: 0, name: myapp.ui, handlers:  level: 0, name: myapp, handlers: [
] level: 30, name: root, handlers: 
From this output it becomes obvious that all loggers use a level of 30, since their log levels are 0, which means the look up the hierarchy for the first logger with a non-zero level. I've also not configured any handlers. If I was seeing double output, it's probably because there is more than one handler configured.
- When you log a message, the level is only checked at the logger you logged the message against. If it passes, every handler on every logger up the hierarchy is called, regardless of that logger's level.
- By default, loggers have a level of 0. This means they use the log level of the first parent logger that has an actual level set. This is determined at message-time, not when the logger is created.
- Don't log directly against the root logger. That means: no
logging.basicConfig()and no usage of module-level loggers such as
logging.warning(), as they have unintended side-effects.
- Create a uniquely named top-level logger for your application / library and put all child loggers under that logger. Configure a handler for output on the top-level logger for your application. Don't configure a level on your loggers, so that you can set a level at any point in the hierarchy and get logging output at that level for all underlying loggers. Note that this is an appropriate strategy for how I usually structure my programs. It might not be for you.
- The easiest way that's usually correct is to use
__name__as the logger name:
log = logging.getLogger(__name__). This uses the module hierarchy as the name, which is generally what you want.
- Read the entire logging HOWTO and specifically the Advanced Logging Tutorial, because it really should be called "logging basics".