Logging module: used for log processing work
Basic types of log information:
ImportLogginglogging.debug ('Debug Messages')#Troubleshooting InformationLogging.info ('Info Messages')#Normal interaction InformationLogging.warning ('Warning Messages')#warning MessageLogging.error ('error Messages')#error MessageLogging.critical ('Critical Messages')#Critical Error message
After running the console output:
WARNING:root:warning messagesERROR:root:error messagesCRITICAL:root:critical Messages
These kinds of information are of a level, default DEBUG and info levels do not output, level order of log information: CRITICAL > ERROR > WARNING > INFO > DEBUG.
Two ways to configure:
Function configuration:
Logging.basicconfig (level=logging. DEBUG, Format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', Datefmt='%a,%d%b%Y%h:%m:%s', filename='Log.log', FileMode='W') Logging.debug ('Debug Messages')#Troubleshooting InformationLogging.info ('Info Messages')#Normal interaction InformationLogging.warning ('Warning Messages')#warning MessageLogging.error ('error Messages')#error MessageLogging.critical ('Critical Messages')#Critical Error message
Features:
Basicconfig: The function is relatively simple, can do more things less 1, cannot specify the encoding method of the file 2, cannot simultaneously make the file input and the screen output
Object configuration:
Logger = Logging.getlogger ()#Create a Log Action objectFilehandler = logging. Filehandler ('Log2.log', encoding='Utf-8')#Create a log file action handleStreamhandler = logging. Streamhandler ()#Create a console action handle#specify the format of the logFormatter = logging. Formatter ('% (asctime) s-% (name) s-[line:% (Lineno) d]% (levelname) s% (message) s') Logger.setlevel (logging. DEBUG)#set the log levelStreamhandler.setlevel (Logging.info)#The log level of the individual settings screen cannot be less than logger.setlevel (logging. Level of DEBUG)Filehandler.setformatter (Formatter)#Associated FormatsStreamhandler.setformatter (Formatter)#Associated FormatsLogger.addhandler (Filehandler)#Log Action Object Association fileLogger.addhandler (Streamhandler)#Log Action Object Association screenLogger.debug ('Debug Messages') Logger.info ('Info Messages') logger.warning ('Warning Messages') Logger.error ('error Messages') logger.critical ('Critical Messages')
Features:
1, flexible, strong customization 2, the program is fully decoupled
Configuration parameters:
The logging.basicconfig () function can change the default behavior of the logging module through specific parameters, with the available parameters: FileName: Creates a filedhandler with the specified file name so that the log is stored in the specified files. FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W". Format: Specifies the log display format used by handler. DATEFMT: Specifies the date time format. Level: Set Rootlogger (The following explains the specific concept) stream: Create Streamhandler with the specified stream. You can specify output to Sys.stderr,sys.stdout or file (f=open (' Test.log ', ' W ')), default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored. formatting strings that may be used in the format parameter:%(name) s Logger's name%(Levelno) s log level in digital form%(levelname) s log level in text form%(pathname) s The full pathname of the module that invokes the log output function, possibly without%(filename) s The file name of the module that invokes the log output function%module Name of the log output function called by (module) s%(funcName) s function name of the call log output function%(Lineno) d The line of code where the statement that invokes the log output function%(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 ID. Probably not .% (message) s user-output message
Parameters
Logging module of the Python module