1. List all status and print to terminal.
ImportLogginglogging.debug ("Test Debug")#debugging, error-removingLogging.info ("Test")#Informationlogging.warning ("user [Xsk] 3")#WarningLogging.error ("Test Error")#Errorlogging.critical ("Server is down")#Serious
Note: Serious warnings are printed directly to the terminal, which is prioritized down to the next level.
2. Print the log to a file.
Import logginglogging.basicconfig (FileName='example.log', Level=logging.info )
Note: The log file is automatically created and the data is automatically printed to the file.
Note: Logging.basicconfig (): Creates a log file.
Note: filename=: Enter the log file name.
Note: level=: Enter the alarm level.
Note: When entering, high priority will not output the low priority alarm information.
3. When logging, add time.
Import logginglogging.basicconfig (FileName='example.log', Level=logging.info, format='% (asctime) s% (message) s', datefmt='%m/%d/%y%i:%m:%s%p ')
Note: You can customize the run format by adding the time log format variable.
Note: format= ': The module parameter used to add the logging format.
Note: datefmt= ': Time parameter.
4, so that the log, defined as terminal files can be output.
ImportLogginglogger= Logging.getlogger ('Test-log')#Custom NameLogger.setlevel (logging. DEBUG)#set the lowest levelCH= Logging. Streamhandler ()#Terminal output LogCh.setlevel (logging. WARNING)#Minimum finite levelFH= Logging. Filehandler ("Access.log", encoding="Utf-8")#log within the fileFh.setlevel (logging. ERROR)#Minimum finite levelFh_formatter= Logging. Formatter ('% (asctime) s% (filename) s:% (Lineno) d% (module) s-% (levelname) s% (message) s')#FH Log FormatCh_formatter= Logging. Formatter ('% (asctime) s% (filename) s:% (Lineno) d% (module) s-% (levelname) s% (message) s')#CH Log FormatFh.setformatter (fh.formatter)#bound FH HandlerCh.setformatter (ch_formatter)#Binding CH HandlerLogger.addhandler (FH)#Logger bound FH Handlerlogger.addhandler (CH)#Logger Binding CH Handlerlogger.warning ("DDD")#OutputLogger.error ("error happend.")#Output v
Note: The Logger,handler is bound to the formatter.
5. Define the log custom delete.
(1) According to the file size, delete.
ImportLogging fromLoggingImportHandlerslogger= Logging.getlogger ('TEST') Log_file="Timelog.log"FH= Handlers. Rotatingfilehandler (filename=log_file,maxbytes=10,backupcount=3,encoding="Utf-8") Formatter= Logging. Formatter ('% (asctime) s% (filename) s:% (Lineno) d% (module) s-% (levelname) s% (message) s') Fh.setformatter (formatter) logger.addhandler (FH) logger.warning ("test1") logger.warning ("test2") logger.warning ("test3")
Note: You need to use the Handlers module
Note: Handlers. Rotatingfilehandler (): To be defined.
Filename=: File name
maxbytes=: Define the maximum value
Backupcount=: Defines the maximum number of files backed up
Encodoing=: Defines the character encoding.
(2) According to the time, delete.
ImportLogging fromLoggingImportHandlerslogger= Logging.getlogger ('TEST') Log_file="Timelog.log"FH= Handlers. Timedrotatingfilehandler (filename=log_file,when="S", interval=5,backupcount=3) Formatter= Logging. Formatter ('% (asctime) s% (filename) s:% (Lineno) d% (module) s-% (levelname) s% (message) s') Fh.setformatter (formatter) logger.addhandler (FH)Importtimelogger.warning ("test1") Time.sleep (2) logger.warning ("test2") Time.sleep (2) logger.warning ("test3") Time.sleep (2) logger.warning ("test4")
Note: A log is backed up every 5 seconds
Note: Handlers. Timedrotatingfilehandler (): Implement time Backup Delete.
wen=: Select the time type parameter.
Interval=: Select the number of corresponding parameters.
Backupcount=: Defines the maximum number of files backed up
Encodoing=: Defining character encodings
Python Logging Module