Search code examples
pythonloggingjython

Logging messages appear twice in console Python


I have found this answer to a seemingly similar issue, however (since I'm novice into Python) I am not sure how to implement this solution in my code (if it's the same issue after all).

In my code I have the following section:

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    filename='C:\\Tests\\TRACE.log',
                    filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
consoleFormatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(consoleFormatter)
logging.getLogger('').addHandler(console)
localLog = logging.getLogger('text')

The funny thing is that it used to work fine but at some moment it started writing these duplicate messages to console.

Could someone give me a direction here please?


Solution

  • It seems that I have figured out the source of this problem.

    The thing is that I used to get logger at module level. It looked pretty logical but there is a pitfall – Python logging module respects all created logger before you load the configuration from a file. So basically, when I was importing a module(which uses gets logger internally) to a main code(where I was calling for a logger as well) it resulted in streaming the logger data twice.

    The possible solutions to this problem are:

    1. Do not get logger at the module level
    2. Set disable_existing_loggers to False. Added since Python 2.7