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.
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.
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().
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With