I. Introduction and use of logging module
Python uses the logging module to log logs involving four main classes, each of which uses the following functions:
- Logger: Provides an interface that the application can use directly
Handler: Send (logger created) log records to the output of the appropriate destination
SetLevel: Provides a fine-grained device to determine which level of logging to output
Formatter: Determines the final output form of the log record
two. Logging Content
Logging module--mainly for the following three aspects:
Log level
Log Content Format
Log output mode
1. Log Level
Critical > Error > Warning > Info > Debug
Higher levels print fewer logs and vice versa.
Debug: Print all the logs
- Info: Print info,warning,error,critical-level logs
Warning: Print logs for warning,error,critical boundaries
Error: Print error,critical-level logs
Critical: Print critical-level logs
2. Introduction of Logger
Logger is a subsequent program can be directly called interface, if not to create a logger, then the default call Root-logger (logging), because logger in the log system is the parent and child, if not create a new logger child, When you print the log, it is called directly to the parent.
Common methods: GetLogger
Let's look at a simple example:
Import= Logging.getlogger ("autotest")# There is no log output for the info level, the default logging is the warning level logger.info (" I am a log of type info ") Logger.warning (" I am a warning type of log ") logger.error (" I am the wrong type of log output ")
The result is this:
3. Introduction of Setlevel
We generally use Setlevel to set the output level of the log
1) logging has the following levels: DEBUG, INFO, WARNING, ERROR, CRITICAL---> level are getting higher. The default level is warning
2) The logging module will only output log above the specified level
3) Usage logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical (): For log output, equivalent to the role of print
The advantage of this is that in the project development when the debug log, we want to what level of log, do not need to change the code, just need to change the output level of the log in this level of settings can be
4. Introduction of Handler
Streamhandler:
The log information is output to the specified stream, and if the stream is empty the default output is to Sys.stdeer, which is the console
Filehandler:
Inherit from Streamhandler, output log information to disk file, by default, log file can grow infinitely
The handlers module in the logging module is specifically designed to handle log output at Logging.handlers
Common handlers:
Rotatingfilehandler: Supports circular log files
MaxBytes: Allows log files to rollover when MaxBytes is reached. A new log file is created when the file size reaches or exceeds MaxBytes
Backupcount: The number of backups, that is, how many backups can be. The name is appended with the. 0-.N suffix (e.g. test.0.log, test.1.log ...) after the base_name of the log.
Timedrotatingfilehandler: On the basis of rotatingfilehandler, it supports timing generation of new log files. When: type of time interval
5. Introduction of Formatter
Logging. Formatter: Used to set the rendering of log content in the console or in a file:
There are two parameters when creating the Formatter object:
FMT: What is specified in the log, the time the log was generated, where the file is located, the line of code, etc.
DATEFMT: The format of the time. The default format is:%y-%m-%d%h:%m:%s
Note: There are options in the formatter class that describe the FMT option
6. Example
ImportLogging fromLogging.handlersImportRotatingfilehandler#First step: Create a log collector loggerLogger = Logging.getlogger ("autotest")#Step Two: Modify the log output levellogger.setlevel (logging.info)#Step Three: Format the output log contentFMT ='% (asctime) s% (filename) s% (funcName) s [line:% (Lineno) d]% (levelname) s% (message) s'datefmt='%a,%d%b%Y%h:%m:%s'FM=logging. Formatter (FMT, datefmt)#Fourth Step: Set the output channel-output to file handlerHd_1 = Rotatingfilehandler ("Autotest.log", Maxbytes=1024*1024*2, backupcount=10, encoding="Utf-8")#Specify the log content format on handlerhd_1.setformatter (FM)#设置output channel-output to consoleHd_2 =Logging. Streamhandler ()
# Specify the log content format on handler hd_2.setformatter (FM)#Fifth Step: Add Headler to log loggerLogger.addhandler (hd_1) Logger.addhandler (hd_2)#Sixth step: Call the Output methodLogger.info ("Hahahahahhahahah---") logger.warning ("I'm a warning type of log") Logger.error ("I am the wrong type of log output") Logger.info ("Hehehehhehe")
File output results:
Console output Results:
7. Summary
Python's logging module