Python comes with a log processing module logging
The default log level is debug,info,warning,error,critical, the corresponding function is debug (), INFO (), WARNING (), ERROR (), and CRITICAL ()
In [490]: Import Loggingin [491]: log_filename= '/tmp/example.log ' in [492]: Logging.basicconfig (Filename=log_filename, Level=logging. DEBUG) in [493]: logging.debug (' This message should go to the log file ')
View/tmp/example.log's content
$ cat/tmp/example.log DEBUG:root:This message should go to the log file
Continuous implementation
Logging.debug (' This message should go to the log file ')
/tmp/example.log will continue to refresh
Log rotation
in [639]: import globin [640]: import loggingin [641]: import Logging.handlersin [642]: log_filename= '/tmp/logging_rotatingfile_example.out ' In [643]: my _logger=logging.getlogger (' MyLogger ') in [644]: my_logger.setlevel (logging. DEBUG) In [645]: handler=logging.handlers.rotatingfilehandler (log_filename,maxbytes=20,backupcount=5) In [646]: my_logger.addhandler (handler) In [648]: for i in range (: ) my_logger.debug (' i=%d ' % i) .....: .....: in [650]: logfiles=glob.glob ('%s* ' %LOG_ FILENAME) in [651]: for filename in logfiles: .....: print filename .....: .....: /tmp/logging_rotatingfile_example.out.1/tmp/logging_rotatingfile_example.out.2/tmp/logging_rotatingfile_example.out.5/tmp/ Logging_rotatingfile_example.out.3/tmp/logging_rotatingfile_example.out.4/tmp/logging_rotatingfile_example.out
The most recent log file is always named Logging_rotatingfile_example.out, which is rotated once the size reaches the 20 bytes of the MaxBytes setting.
One of the most important features of the logging module is the ability to output different log information based on the different log levels set.
Reference Documentation:
Https://docs.python.org/2.6/library/logging.html#configuration-file-format
This article is from the Linux SA John blog, so be sure to keep this source http://john88wang.blog.51cto.com/2165294/1622501
Python Learning logging Module