Goku
Goku

Reputation: 534

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, the rest can be at INFO level.

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

Current configuration looks like this;

import logging

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

...

Upvotes: 2

Views: 37

Answers (1)

Sociopath
Sociopath

Reputation: 13401

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

Upvotes: 2

Related Questions