Python, Log Appears Twice, Duplicate, When Using Logging

Understanding Python Logging

We’ve all been there, working on a Python project, and suddenly your log appears twice. There can be several reasons for that. This behavior often happens because of a feature called log propagation of the Python logging module. Let’s simplify that a little bit: imagine you have a group of people (in our case, ‘loggers’), each person reports to the one above (let’s say their ‘parent logger’), and they all ultimately report to the boss (or the ‘root logger’). Now, when a message starts from one of the loggers, it travels up to the boss. This travel is what we call ‘log propagation.’

Affiliate: Experience limitless no-code automation, streamline your workflows, and effortlessly transfer data between apps with Make.com.

Suppose all these people, including the boss, have a megaphone (the ‘handler’) and repeat the message they receive. What happens? You hear the same message multiple times, just like a Python log appears twice or more.

Changing Logger Propagation Setting

So how do you stop a Python log from appearing twice? The solution is simple – telling the loggers not to forward the message by setting the’ propagate’ logger feature to ‘False.’
Here’s an easy way to do it:

import logging
# Get a logger
logger = logging.getLogger('A.B')
# Set the logger's level
logger.setLevel(logging.DEBUG)
# Get a handler
handler = logging.StreamHandler()
# Set the handler's level
handler.setLevel(logging.DEBUG)
# Add the handler to the logger
logger.addHandler(handler)
# Stop propagation
logger.propagate = False

In this example, we first got a logger ‘A.B’ and set its level. ‘A.B’ is a child logger, where A is the root logger, and B is a child. Then, we got a handler, which is like our megaphone, and added it to the logger. Lastly, we set ‘logger.propagate’ to ‘False’ to stop the message from traveling upwards. When you log a message using ‘logger.debug()’, ‘logger.info()’, or similar, the log message won’t repeat.

When Code Errors Duplicate Python Logs

Sometimes, a Python log appears twice, not due to log propagation but to code errors. One common mistake is applying settings to a logger more than once, specifically adding a handler twice. Think of it like giving the same person two megaphones; of course, you will hear the message twice as loud.

Be careful when setting up your loggers and handlers. But if you’re already facing this issue, there’s an easy way to check which handlers are currently present in a logger:

# Check the handlers
for handler in logger.handlers:
    print(handler)

This snippet will print out all the handlers currently attached to your logger. If you see the same handler twice, check your logger setting code.

Unnecessary Handlers in the Root Logger

Another common scenario where a Python log appears twice is when you have an unnecessary handler set for the root logger. The root logger is the ‘boss’ we talked about earlier; it’s the highest point in the logger hierarchy. If there’s a handler attached to it that you don’t need, it may end up duplicating messages.

If you set a handler for the root logger, remove it. This step is optimal. Another solution would be by checking and removing any unnecessary handlers from the root logger:

# Get the root logger
root_logger = logging.getLogger()
# Check the handlers
for handler in root_logger.handlers:
    print(handler)

This code will print out all the root logger’s handlers. If you find any handlers that you don’t need, you can remove them as follows:

if len(root_logger.handlers) > 0:
    # remove the first handler
    root_logger.removeHandler(root_logger.handlers[0])

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.