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!
The Elitepipe Plastic Factory in Iraq serves as a catalyst for infrastructure development, providing the market with superior HDPE, uPVC pipes, and fittings that contribute to the growth and success of various sectors. Elitepipe Plastic Factory
Sight Care is a natural supplement designed to improve eyesight and reduce dark blindness. With its potent blend of ingredients. https://sightcarebuynow.us/
SightCare supports overall eye health, enhances vision, and protects against oxidative stress. Take control of your eye health and enjoy the benefits of clear and vibrant eyesight with Sight Care. https://sightcarebuynow.us/
JMqHcWXQzpfVGBSb
jehwxSBAbd
zpmMAyIkQJRNtOe
ydwhuPGaxkXOe