One, log level
The log is divided into 5 levels, from low to High, respectively: DEBUG INFO WARNING ERROR CRITICAL.
DEBUG: Detailed information, usually appearing only on diagnostic issues
INFO: Make sure everything works as expected
WARNING: A sign that some unexpected things have happened, or indicate some problems in the near future (for example. Low disk space "). The software will work as expected.
ERROR: A more serious problem, the software does not perform some functions
CRITICAL: A serious error that indicates that the program itself may not continue to run
These 5 levels, also correspond to 5 kinds of methods to hit the log: Debug, info, warning, error, critical. The default is warning, which is tracked only when it is warning or above.
Ii. several concepts
The Logger Recorder exposes an interface that the application code can use directly.
Handler processor that sends (logger-generated) log records to the appropriate destination.
Filter Filters provide better granularity control, which determines which log records are output.
The Formatter formatter , which indicates the layout of the log records in the final output.
#The first step is to create a loggerLogger =Logging.getlogger () logger.setlevel (logging.info)#log level master switch #The second step is to create a handler for writing to the log fileLogFile ='./log/logger.txt'FH= Logging. Filehandler (logfile, mode='W') Fh.setlevel (logging. DEBUG)#switch output to file's log level #The third step is to create a handler for output to the consoleCH =logging. Streamhandler () Ch.setlevel (logging. WARNING)#switch output to the log level of the console #Fourth step, define the output format of the handlerFormatter = logging. Formatter ("% (asctime) s-% (filename) s[line:% (Lineno) d]-% (levelname) s:% (message) s") Fh.setformatter (formatter) ch.setformatter (formatter)#Fifth Step, add logger to handler insideLogger.addhandler (FH) logger.addhandler (CH)#LogLogger.debug ('This is a logger debug message') Logger.info ('This is a logger info message') logger.warning ('This is a logger warning message') Logger.error ('This is a logger error message') logger.critical ('This is a logger critical message')
The simple understanding is that if you want to call write log to create a logger recorder, the logger recorder can contain one or more handler processors to determine where the log output, handler to add formatter settings output format.
formatting strings that may be used in the format parameter:
% (name) s logger name
% (Levelno) s log level in digital form
% (levelname) s log level in text form
% (pathname) s calls the full pathname of the module of the log output function and may not have
% (filename) s The file name of the module that called the log output function
% (module) s call the module name of the log output function
% (FuncName) s Call the function name of the log output function
% (Lineno) d The line of code where the statement of the log output function is called
% (created) F current time, represented by the UNIX standard floating-point number representing the time
% (relativecreated) d when the log information is output, the number of milliseconds since logger was created
% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d thread ID. Probably not.
% (threadname) s thread name. Probably not.
% (process) d process ID. Probably not.
% (message) s user-output message
Python Learning note (logging module)