The Python standard log module uses Handler control log messages to write to different destinations, such as files, streams, messages, sockets, and so on. In addition to the Streamhandler, Filehandler, and Nullhandler definitions in the logging module, the other Handler are defined in the Logging.hangdlers module. These Handler are: Watchedfilehandler, Rotatingfilehandler, Timedrotatingfilehandler, Sockethandler, Datagramhandler, Sysloghandler, Nteventloghandler, Smtphandler, Memoryhandler, HttpHandler.
Rotatingfilehandler is able to manage the size of the log file, the log file size reaches the threshold, it renames the logfile, and then creates a new log file with the same name to continue the output.
ImportLoggingImportLogging.handlershandler=logging.handlers.RotatingFileHandler (filename='Myapp.log', MaxBytes= 200, Backupcount= 5,) Handler.setformatter (logging. Formatter (FMT='% (asctime) s [% (ThreadName) s] (% (filename) s:% (lineno) d)% (levelname) s-% (message) s', Datefmt='%y-%m-%d%h:%m:%s')) Root=Logging.getlogger () root.setlevel (logging. DEBUG) Root.addhandler (handler) forIinchRange (20): Root.debug ('The Message (%D/20).'% (i + 1))
Parameter MaxBytes set the threshold value of log file size, parameter Backupcount set the number of backup files. When the log file is myapp.log to a threshold of 200 bytes, Rotatingfilehandler renames myapp.log to Myapp.log.1 and then creates a new logfile Myapp.log with the same name. When the size of the Myapp.log reaches 200 bytes, Rotatingfilehandler will rename the myapp.log.1 to myapp.log.2 and then rename the Myapp.log to Myapp.log.1, and then create a new log file with the same name Myapp.log. This repeats until 5 backup files are created, and Rotatingfilehandler does not rename myapp.log.5 to myapp.log.6 but deletes them when the size of the Myapp.log again reaches 200 bytes.
Timedrotatingfilehandler and Rotatingfilehandler are similar, however, Timedrotatingfilehandler did not determine when to recreate the log file by judging the size of the file. Instead, a new log file is created automatically at a certain time interval. During renaming, the suffix of the backup log file is not a number, but the current time.
ImportLoggingImportlogging.handlersImportTimehandler=logging.handlers.TimedRotatingFileHandler (filename='Myapp.log', when='M', Interval= 2, Backupcount= 3,) Handler.setformatter (logging. Formatter (FMT='% (asctime) s [% (ThreadName) s] (% (filename) s:% (lineno) d)% (levelname) s-% (message) s', Datefmt='%y-%m-%d%h:%m:%s')) Root=Logging.getlogger () root.setlevel (logging. DEBUG) Root.addhandler (handler) forIinchRange (20): Root.debug ('The Message (%D/20).'% (i + 1))) Time.sleep (30)
The parameter interval sets the time interval, and when the parameter is set the unit of the interval. When the value can be ' s ' (seconds), ' M ' (minutes), ' H ' (Hours), ' D ' (days), ' W0 '-' W6 ' (Monday to Saturday), ' Midnight ' (wee), when the value is ' W0 '-' W6 ' or ' midnight ', the parameters The value of the interval is ignored.
Smtphandler can send log messages to the mail server via SMTP.
ImportLoggingImportLogging.handlershandler=Logging.handlers.SMTPHandler (Mailhost= ('smtp.163.com', 25), Fromaddr='[email protected]', Toaddrs='[email protected]', Subject='Smtphandler Demo', Credentials= ('[email protected]','123456') ) Handler.setlevel (logging. CRITICAL) Handler.setformatter (logging. Formatter (FMT='% (asctime) s [% (ThreadName) s] (% (filename) s:% (lineno) d)% (levelname) s-% (message) s', Datefmt='%y-%m-%d%h:%m:%s')) Root=Logging.getlogger () Root.addhandler (handler) root.critical ('This is a critical message');
Additional Handler usage examples are to be added.
Python (2.7.6) standard log module-Logging Handler