first, Basic Usage
Only the basic configuration is required and can be used.
import loggingdef fun2 (): logging.basicconfig (filename="fun2.log", format ="% (asctime) s% (message) s", level=logging. DEBUG) logging.debug ("This isfun2 log")
second, the detailed configuration
First add a Filehandler to configure the record of the file, formatter to set the format of the record and time format, GetLogger is to get a specified name logger, and then add handler to this logger, and set the record level, It can then be recorded at the appropriate level.
import loggingdef fun1 (): logname="Test.log"Filehandler= Logging. Filehandler (filename=logname,encoding="Utf-8") Fmter= Logging. Formatter (fmt="% (asctime) s% (message) s", datefmt="%y-%m-%d%h:%m:%s") filehandler.setformatter (fmter) loger=logging.getlogger (__name__) loger.addhandler (filehandler) loger.setlevel (logging. FATAL) Loger.fatal ("Second Log")
Where the formatter configuration parameter FMT has the following optional parameters
%(name) s name of the logger (logging Channel)% (levelno) S Numeric logging level forthe message (DEBUG, INFO, WARNING, ERROR, CRITICAL)% (levelname) S Text logging level forThe Message ("DEBUG","INFO", "WARNING","ERROR","CRITICAL") % (pathname) s full pathname of the source filewhereThe logging call was issued (ifAvailable)%(filename) s filename portion of pathname%(module) s Module (name portion of Filename)% (lineno) D Source line numberwhereThe logging call was issued (ifAvailable)%(funcName) s Function name%(created) F time when the LogRecord is created (time.time ()returnValue)%(asctime) s textual time when the LogRecord is created%(msecs) d millisecond portion of the creation time% (relativecreated) D timeinchmilliseconds when the LogRecord is created, relative to the time of the logging module was Loade D (typically at application startup Time)% (thread) D thread ID (ifAvailable)% (threadname) S Thread name (ifAvailable)% (process) D process ID (ifAvailable)% (message) s The result of Record.getmessage (), computed just asthe Record isEmitted
All of the following are transferred from the Python logging module
third, Additional Instructions
1 The log levels available for selection are
ten = 0
Logger.debug (), logger.info (), logger.warning (), logger.error (), logger.critical () output different levels of logging, Only logs with log levels greater than or equal to the set log level will be Output.
2 Handler
The handler object is responsible for sending the relevant information to the specified destination, commonly used in the following ways
Handler.setlevel (lel): Specify log level, logs below lel level are ignored
Handler.setformatter (): Choose a formatter for this Handler
Handler.addfilter (filt), handler.removefilter (filt): Add or remove a filter object
You can add multiple handler to logger by using the AddHandler () method
Logging.handlers.RotatingFileHandler is similar to the Filehandler above, but it can manage file Sizes. When the file reaches a certain size, it automatically renames the current log file and creates a new log file with the same name to continue the output
Logging.handlers.TimedRotatingFileHandler is similar to rotatingfilehandler, however, it does not determine when the log file is recreated by judging the file Size. instead, A new log file is created automatically at a certain interval of time
Logging.handlers.SocketHandler uses the TCP protocol to send log information to the Network.
Logging.handlers.DatagramHandler uses the UDP protocol to send log information to the Network.
Logging.handlers.SysLogHandler log output to Syslog
Logging.handlers.NTEventLogHandler remote output log to Windows NT/2000/XP event log
Logging.handlers.SMTPHandler remote output log to e-mail address
Logging.handlers.MemoryHandler log output to in-memory development buffer
Logging.handlers.HTTPHandler remote output to the HTTP server via "GET" or "POST"
The specific usage of each handler can be viewed in the bibliography:
Https://docs.python.org/2/library/logging.handlers.html#module-logging.handlers
Use of the logging module in Python