Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set two different logging levels

I am using a basic logging config where all messages of level INFO are getting displayed to stdout, but I’m looking to set log level of WARNING only for the Kafka related messages, rest can be at INFO level.

I’m not sure how to set two different logging levels with minimum settings.

Current configuration looks like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import logging

logging.basicConfig(format='[%(asctime)s] [%(levelname)s] - %(message)s', level=logging.INFO)

...

>Solution :

You can use logger object and create multiple loggers as below

import logging

logging.basicConfig(filename="newfile.log",
                    format='[%(asctime)s] [%(levelname)s] - %(message)s',
                    filemode='w')

# Creating an object
logger1 = logging.getLogger("logger1")
logger2 = logging.getLogger("logger2")
 
# Setting the threshold of logger
logger1.setLevel(logging.INFO)
logger2.setLevel(logging.WARNING)

logger1.debug("THis is logger1 Debug")
logger1.info("THis is logger1 INFO")
logger2.debug("THis is logger2 Debug")
logger2.info("THis is logger2 INFO")
logger2.warning("THis is logger2 warning")

Output

INFO:logger1:THis is logger1 INFO
WARNING:logger2:THis is logger2 warning
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading