There are four main classes of logging module logging: Logger,handler,formatter,filter
Logger provides an interface that the application can use directly, and each program obtains a logger before outputting the information
Handler sends the log record (created by logger) to the appropriate destination output, handler can output the information to the console, or output the information to a file, and send the information to the network
Formatter determining the final output format of the log record
The filter is used to filter the log records, and the filter function returns a Boolean value that logger the filtered statement based on the returned Boolean value.
Write log files at the same time as the screen print log:
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import Loggingclass ignorebackuplogfilter (logging. Filter): "Ignore log with warning" ' Def filter (self, record): #固定写法 return ' warning ' not in record.getmessage () #返回布 Type logger = Logging.getlogger (' web ') #生成一个Logger对象, the web is the name of the Logger object Logger.setlevel (logging.info) #设置等级, The default is Warningstream_handler = logging. Streamhandler () #生成handler对象 for on-screen printing File_handler = logging. Filehandler (' Web.log ') #生成handler对象. Used to generate the log file # Bind the handler object to Loggerlogger.addhandler (Stream_handler) Logger.addhandler (File_handler) # Logger.removehandler (Stream_handler) #删除handler对象 # Logger.removehandler (file_handler) #生成formatter对象, For on-screen printing stream_formatter = logging. Formatter ('% (name) s-% (asctime) s-% (levelname) s-% (message) s ') #生成formatter对象, used for log file File_formatter = logging. Formatter ('% (name) s-% (asctime) s-% (levelno) s-% (message) s ') #把formatter对象到handler对象stream_handler. Setformatter ( Stream_formatter) File_handler.setformatter (file_formatter) logger.addfilter (ignorebackuplogfiltER ()) #对日志内容进行过滤 #logger.removefilter (Ignorebackuplogfilter ()) #删除filter对象logger. Debug (' The Debug ') Logger.info (' The info ') logger.warning (' The Warning ') logger.error (' the error ') logger.critical (' The Critical ')
Run results
Log content
Log truncation based on file size
If the log file is Web.log, when the web.log reaches the specified size, Rotatingfilehandler automatically renames the file to Web.log.1, if web.log.1 already exists, the Web.log.1 is renamed to Web.log.2, and the web is finally recreated . log, continue to output log information
Handlers. Rotatingfilehandler (filename[, mode[, maxbytes[, Backupcount]])
FileName and mode two parameters and Filehandler, maxbytes as the maximum size of the log file, if the MaxBytes is 0, meaning that the log file can be infinitely large, no longer create a new log file. Backupcount for the maximum number of backup log files generated, if specified as 3, up to 3 backup log files, if the backup log file has been generated 3, and then generate the log file, web.log.3 will not be renamed, but deleted
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import loggingfrom logging Import Handlerslogger = Logging.getlogger (' Web ') Logger.setlevel (logging.info) File_handler = handlers. Rotatingfilehandler (' Web.log ', maxbytes=5,backupcount=3) Logger.addhandler (file_handler) File_formatter = logging. Formatter ('% (name) s-% (asctime) s-% (levelno) s-% (message) s ') File_handler.setformatter (file_formatter) logger.debug (' The Debug ') logger.info (' the info ') logger.warning (' The Warning ') logger.error (' the error ') logger.critical (' The Critical ')
Run
Generated 4 log files, Web.log to the latest log, web.log.3 as the oldest log
Based on time
A new log file is created automatically at the time specified by the interval, and the rename process is similar to Rotatingfilehandler, although the new file is named the current time
Handlers. Timedrotatingfilehandler (filename [, when [, interval [, Backupcount]])
The filename parameter and the Backupcount parameter have the same effect as the Rotatingfilehandler
The When parameter is a string. A unit that represents a time interval, not case-sensitive. S is seconds, M is minutes, h is hour, D is day, W is per week (Interval=0 is Monday), midnight is every morning
Interval for time interval
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import loggingfrom logging Import Handlerslogger = Logging.getlogger (' Web ') Logger.setlevel (logging.info) File_handler = handlers. Timedrotatingfilehandler (' Web.log ', when= ' s ', interval=3,backupcount=5) Logger.addhandler (File_handler) file_ Formatter = logging. Formatter ('% (name) s-% (asctime) s-% (levelno) s-% (message) s ') File_handler.setformatter (file_formatter) logger.debug (' The Debug ') logger.info (' the info ') logger.warning (' The Warning ') logger.error (' the error ') logger.critical (' The Critical ')
Generates one every 5 seconds and needs to be run before it is generated
Run results
Grade
Logger is set to the highest level, which is the main script
The level level set in handler is lower than the level level set by logger
So the log printed in handler is set at the logger level, and then the level of the handler setting is considered.
Python module-logging module (ii)