1. Logging module Log level
A simple example of using the logging module:
>>>import logging>>>logging.debug ("This ' s a Test Logging") >>>loggin.info ("Still a test Logging ") # The above two lines do not have output by default, you need to specify the log level to line >>>logging.warn (" It's waring!!! ")
Logging module Log level:
| Log Level |
value |
| CRITICAL |
50 |
| ERROR |
40 |
| WARNING |
30 |
| INFO |
20 |
| DEBUG |
10 |
| UNSET |
0 |
For a level of log messages, this log message is published only if the processor and the logger are configured to publish the level (or higher). For example, if the level of a message is critical, the level of log configuration is error, and this message is emitted (50>40). If the message is warning and the logger is set to error, the message will not be emitted (30<40).
2, logging module Basic understanding of the use
Different priorities correspond to different functions: Logging.debug,logging.warn and so on.
One or more logger objects that use the module primarily through them
Write a message to a terminal or file, database, or other place handler
Create the formatter of the output
Filter based on input
First, set the logging log default priority:
默认级别是WARNING,设置日志默认优先级为DEBUG
>>>import Logging>>>logging.basicconfig (level=logging. DEBUG)
Second, create logger object
每个logger对象都有一个名称,比如创建一个test的logger
>>>import logging>>>logging.basicconfig (level= ' DEBUG ') >>>logger = Longgin.getLogger (' Test ') >>>logger.debug (' The Great gates! ') DEBUG:test:the Great gates!
Third, use handler to output the message to the log file
>>>import logging>>>logging.basicconfig (level= ' DEBUG ', filename= ' test.log ') >>>logger = Logging.getlogger (' TX ') >>>logger.war (' Xxxxssss ')
Iv. format of control messages
>>>import logging>>>fmt = '% (asctime) s% (levelname) s% (Lineno) s% (message) s ' >>> Logging.basicconfig (lebel= ' DEBUG ', filename= ' Test.log ', format=fmt) >>>logger = Logging.getlogger (' xx ') >>>logger.warn (' Where is Loggggg ') 2016-5-5 23:22:11,877 warn 1 where is LOGGGGG
3. log file rotation
To create a new file each time you run the program, you can pass in the value "W" to the parameter FileMode of Basicconfig (). However, it is best not to manage log files this way, it is better to use a rotatingfilehandler, which will automatically create new files while preserving the original files.
>>>import glob>>>import Logging>>>import Logging.handlers>>>logger = Logging.getlogger (' MyLogger ') >>>logger.setlevel (logging. DEBUG) >>>log_filename= ' test.log ' >>>handler = Logging.handlers.RotatingFileHandler (log_filename , maxbytes=100, backupcount=5) >>>logger.addhandler (handler) # View the log file for scroll generation for I in range: Logger.debug (' i =%d '% i) # view those files created logfiles = Glob.glob ('%s * '% log_filename) for filename in logfiles: print Filena Me results: test.logtest.log.1test.log.2...test.log.5 The latest files are always test.log, and each time the size limit is reached, the suffix Plus. 1 is renamed. The existing individual backup files are also renamed, incrementing the suffix (. 1 to. 2, etc.), and. 5 files are deleted
Python Log record-logging module