Many programs have logging requirements, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python's logging module provides a standard log interface, you can store various formats of the log, logging log can be divided into debug(),,, info() warning() and error() critical() 5个级别, below we look at how to use.
I. The simplest usage
Import Logginglogging.debug ('Debug Message') logging.Info('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message') #WARNING: root:warning message#error:root:error message#critical:root:critical message displays only WARNING above the log by default
Two. Modify the default configuration
Import Logging Logging.basicconfig ( level=logging. DEBUG, #这里是日志显示的级别 format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', #这里是打印的格式, what content to display datefmt='%a,%d%b%Y%h:%m:%s', #答应日期的格式 filename='/tmp/test.log', #日志保存的位置, if the filename is not added here, it will be shown in the form of consent, and will not save FileMode='W') #这里是w会重新覆盖, A is an additional logging.debug ('Debug Message') logging.Info('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message')
1 formatting strings that may be used in the format parameter:2%(name) s Logger's name3%(Levelno) s log level in digital form4%(levelname) s log level in text form5%(pathname) s The full pathname of the module that invokes the log output function, possibly without6%(filename) s The file name of the module that invokes the log output function7%module Name of the log output function called by (module) s8%(funcName) s function name of the call log output function9%(Lineno) d The line of code where the statement that invokes the log output functionTen%(created) F current time, represented by the UNIX standard floating-point number representing the time One%(relativecreated) d when the log information is output, the number of milliseconds since logger was created A% (asctime) s The current time in string form. The default format is "2003- -- , -: the: $,896". The comma is followed by milliseconds -%(thread) d thread ID. Probably not . -%(threadname) s thread name. Probably not . the%(process) d ID. Probably not . -% (message) s user-output message
Three. Synchronous display
There is a problem here, the log can only be printed or saved, only select one, we improve
Import Logginglogger=Logging.getlogger () # Create a handler for writing to the log file FH= Logging. Filehandler ('Test.log'# Create another handler for output to the console ch=logging. Streamhandler () Formatter= Logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') Fh.setformatter (formatter) ch.setformatter (formatter) Logger.addhandler (FH) # Logger object can add multiple FH and ch objects logger.addhandler (CH)
Logger.setlevel (logging. Debug) Logger.debug ('Logger Debug Message') logger.Info('Logger Info Message') logger.warning ('Logger warning message') Logger.error ('Logger error message') logger.critical ('Logger Critical Message')
The logging library provides several components:
The Logger object provides an interface that the application can use directly
Handler send logs to the appropriate destination
Filter provides a method for filtering log information
Formatter specifies the log display format.
Python Log logging module