Module-level functions
Logging.getlogger ([name]): Returns a Logger object that returns root if no name is specified logger
Logging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical (): Set the log level for root logger
Logging.basicconfig (): Establishes a streamhandler for the logging system with the default formatter, sets the base configuration and adds it to root logger
Log level
Logging.basicconfig (): Establishes a streamhandler for the logging system with the default formatter, sets the base configuration and adds it to root logger
ImportLoggingImportSyslevels= {'Debug': Logging. DEBUG,'Info': Logging.info,'Warning': Logging. WARNING,'Error': Logging. ERROR,'Critical': Logging. CRITICAL}ifLen (SYS.ARGV) > 1: Level_name= Sys.argv[1] level=Levels.get (level_name, logging. NOTSET) Logging.basicconfig ( level=Level ) Logging.debug ('This is a debug message') Logging.info ('This is an info message') logging.warning ('This is a warning message') Logging.error ('This is an error message') logging.critical ('This is a critical error message')
$ python logging_level_example.py debugDEBUG:root:This isa debug MessageINFO:root:This isAn info messageWARNING:root:This isa warning MessageERROR:root:This isAn error messageCRITICAL:root:This isa critical error message$ python logging_level_example.py infoINFO:root:This isAn info messageWARNING:root:This isa warning MessageERROR:root:This isAn error messageCRITICAL:root:This isA critical error message
Loggers
Logger.setlevel (LEL): Specifies the lowest log level, and the level below LEL is ignored. Debug is the lowest built-in level, critical is the highest
Logger.addfilter (filt), Logger.removefilter (filt): Add or remove the specified filter
Logger.addhandler (HDLR), Logger.removehandler (HDLR): Add or remove the specified handler
Logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical (): Log levels you can set
Handlers
The handler object is responsible for sending relevant information to the specified destination. Multiple handler can be added through the AddHandler () method
Handler.setlevel (LEL): Specifies the level of information being processed, and information below the LEL level is ignored
Handler.setformatter (): Choose a format for this Handler
Handler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter object
Formatters
The formatter object sets the final rules, structure, and contents of the log information, the default time format is%y-%m-%d%h:%m:%s, and the following are some of the information commonly used by formatter
% (name) s |
Logger's name. |
% (Levelno) s |
Log level in digital form |
% (LevelName) s |
Log level in text form |
% (pathname) s |
The full path name of the module that called the log output function 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 |
Function name of the call log output function |
% (Lineno) d |
The line of code where the statement that called the log output function is located |
% (created) f |
Current time, represented by the UNIX standard floating-point number representing the time |
% (relativecreated) d |
The number of milliseconds since logger was created when the log information is output |
% (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 |
The thread ID. Probably not. |
% (ThreadName) s |
The name of the thread. Probably not. |
% (process) d |
The process ID. Probably not. |
% (message) s |
User-Output messages |
Finally encapsulated into a class:
#-*-Coding:utf-8-*-import loggingclass Logger (): Def __init__ (self, logname, loglevel, Logger): " Specifies the file path to save the log, the log level, and the call file to deposit the log into the specified file "' format_dict = {1:logging. Formatter ('% (asctime) s-% (module) s-% (name) s-% (levelname) s-% (message) s '), 2:logging. Formatter ('% (asctime) s-% (module) s-% (name) s-% (levelname) s-% (message) s '), 3:logging. Formatter ('% (asctime) s-% (name) s-% (module) s-% (levelname) s-% (message) s '), 4:logging. Formatter ('% (asctime) s-% (name) s-% (module) s-% (levelname) s-% (message) s '), 5:logging. Formatter ('% (asctime) s-% (name) s-% (module) s-% (levelname) s-% (message) s ')} # Create a logger Self.log GER = Logging.getlogger (logger) self.logger.setLevel (logging. DEBUG) # Creates a handler that is used to write to the log file FH = logging. Filehandler (logname) fh.setlevel (logging. DEBUG) # Then create a handler for output to the console ch = logging. Streamhandler () Ch.setlevel (logging. DEBUG) # defines the output format of the handler # formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') Formatter = Format_dict[int (loglevel)] FH. Setformatter (Formatter) Ch.setformatter (formatter) # Add Logger handler (FH) to Self.logger.addHandler Self.logger.addHandler (CH) def getlog (self): return Self.logger
Python Learning notes-logging module (log)