Last Updated: February 25, 2016
·
5.34K
· Gabriel Falcão

Avoid duplicated logging in python/flask

There is a tricky "feature" in python's logging module. Let's explore it.

Considering the code below


import logging

main_logger = logging.getLogger("mywebapp")
controllers_logger = logging.getLogger("mywebapp.controllers")
models_logger = logging.getLogger("mywebapp.models")

That is really cool for having separate loggers under the same mywebapp namespace, the trick is that when setting up logging, you must set up only the topmost item in the hierarchy:

main_logger.addHandler(some_handler)

make sure to not add handlers to the controllers_logger or to the models_logger