Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging.info() not logging the message

Tags:

python

logging

parser_logger = logging.getLogger("CSHEL_parserlogger");
#logging.basicConfig()
parser_logger.addHandler(RotatingFileHandler(
                            "logfile", mode='a', maxBytes=7340032, backupCount=4,
                            encoding=None, delay=False))

#d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
parser_logger.info('Protocol problem: %s', 'connection reset')

This would create a file named logfile, but won't write anything into it. If I change the last line to

parser_logger.warning('Protocol problem: %s', 'connection reset')

it would log the message into the "logfile" properly.

I am sure it's a petty thing that I am missing, but I am not able to figure out what it is.

like image 704
Harman Avatar asked Oct 19 '11 18:10

Harman


People also ask

How do you show logging information in Python?

Python Logging – INFO Level To log an INFO line using Python Logging, Check if the logger has atleast a logging level of INFO. Use logging.info() method, with the message passed as argument, to print the INFO line to the console or log file.

How do you log error messages in Python?

Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger. The arguments are interpreted as for debug().

How do I print a log message in Python?

Use logging Module to Print Log Message to a File in Python getLogger(name) method. There is a convention to use __name__ variable as the name of the logger. Once we have created a new logger, we should remember to log all our messages using the new logger.info() instead of the root's logging.info() method.

How is logging done in Python?

Python has a built-in module logging which allows writing status messages to a file or any other output streams. The file can contain the information on which part of the code is executed and what problems have been arisen.


1 Answers

You need to set the threshold level of the logger:

parser_logger.setLevel(logging.INFO)

When a logger is created, the level is set to NOTSET, and the root logger is created with level WARNING. See the documentation.

like image 160
Vebjorn Ljosa Avatar answered Sep 21 '22 17:09

Vebjorn Ljosa