program, you need to add a log to record a large amount of information.
ImportLogging#First step: Create LoggerSelf.logger =Logging.getlogger () self.logger.setLevel (logging. NOTSET)#Setting the logger level#Step Two: Create a handler to write to the log fileLog_file_path = Log.get_log_file_path (self)#Get log file pathSelf.filehandler = logging. Filehandler (Log_file_path)#for writing to log files#Step Three: Define the output formatFormatter = logging. Formatter ('>>>% (asctime) s-% (filename) s [line:% (Lineno) d]-% (levelname) s:% (message) s')#Fourth Step: Define the output format of the handler formatterSelf.fileHandler.setFormatter (Formatter)#Fifth Step: Add handler to LoggerSelf.logger.addHandler (Self.filehandler)#Filtering LogsSelf.logger.addFilter (Self.filehandler)
Default configuration
By default, the logging module prints logs to standard output, and only logs at warning and above are output.
#rank sort: CRITICAL > ERROR > WARNING > INFO > DEBUGLogging.debug ('DEBUG: Print all logs, detailed information, usually only on diagnostic issues') Logging.info ('INFO: Print info,warning,error,critical-level logs to make sure everything works as expected') logging.warning ('WARNING: Print warning,error,critical level logs, an indication that some unexpected things have happened, or indicate some problems in the near future (for example. Disk space is low "), the software can work as expected') Logging.error ('ERROR: Print error,critical level of log, more serious problem, the software does not perform some functions') logging.critical ('CRITICAL: Print CRITICAL level, a serious error that indicates that the program itself may not continue to run')
Configure log level, format, output location, etc.
With the Logging.basicconfig () function, you can change the default configuration of the logging module with the following parameters:
Logging.basicconfig (level=logging. NOTSET,#setting the log level for RootloggerFilename='Error.log',#Specify the file name to create the Filedhandler to store the log in the specified fileFilemode='a',#mode, W and a,w are write mode, each time will be re-write the log, overwriting the previous log #a是追加模式, default if not written, is the Append modeformat=">>>% (asctime) s-% (filename) s[line:% (Lineno) d]-% (levelname) s:% (message) s",#Log Output Modedatefmt="%a,%d%b%Y%h:%m:%s" #Set Date 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
Concepts of Class Logger, Handler, Formatter, filter
The logging library provides multiple components: Logger, Handler, Filter, Formatter
Logger: Object provides an interface that the application can use directly
Handler: Send logs to the appropriate destination
Filter: Provides a way to filter log information
Formatter: Specify the Log display format
Multi-Module Use logging
。。。
Python:logging log Processing