Skip to content
Home » Check if Python logger already exists

Check if Python logger already exists

Tags:

I recently got more interested in observability, logging, data quality, etc. For many use cases, you don’t need full-blown observability solutions. Python has robust tools that will help you achieve your logging goals. In this blog post, I discuss a small trick I encountered for using Python’s logging module properly.

Basically, when we usually create a logger, these are the steps we take:

  • we create a logger;
  • we set the level;
  • we create a stream handler;
  • we create a formatter;
  • assign the stream handler, and its format to the logger.
name = 'my_awesome_logger'
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

However, when we are working in notebooks, and rerun our notebook again and again (e.g. for debugging purposes) we keep creating new loggers. However, that’s often not the behavior we’d like to happen: a single logger suffices.

This means we need to check somewhere if the logger already exists. That somewhere is logging’s ‘Manager’ class.

There is [under normal circumstances] just one Manager instance, which holds the hierarchy of loggers.

Logging’s GitHub repository

The Manager instance is only created once and contains all the existing loggers in a dictionary named ‘loggerDict’. Consequently, we only need to match the requested logger name to the existing logger names. We can do this, as follows:

name = 'my_awesome_logger'
if name not in logging.Logger.manager.loggerDict.keys():
    print('Creating new logger.')
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    handler = logging.StreamHandler(sys.stdout)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
else:
    print(f'Logger {name} already exists.')

This script will check the loggerDict’s keys. If it finds a match, it will not create a new logger. Otherwise, it does.

Hope this helps you!

7 thoughts on “Check if Python logger already exists”

Leave a Reply

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