Logging Module
As the name implies, for the program log output, you can set the log level, format, output mode, etc., the basic use method is as follows:
1, Logging.basicconfig Way
simple configuration, can only choose to output to the screen alone or output to a file, cannot be output at the same time. For example:
#-*- coding:utf-8 -*-#只能选择输出到屏幕或文件import logginglogging.basicconfig (level=logging. debug, format= '% (asctime) s % (filename) s[line:% (Lineno) d] % (levelname) s % (message) s ', datefmt= '%a,%d %b %y %h:%m:%s ', filename= ' Test.log ', filemode = ' a ') ' parameter: level: Specifies the log levels of the output format: Specify the log format, including: asctime: Time FileName: Log Attribution file name lineno: The line number of the log corresponding code in the attribution file levelname: Day to the lowest level, Do not specify default to Warning message:Specific log content, DATEFMT: Specify a specific time format, if not specified, Asctime will use the default format, such as: 2018-05-05 22:07:30,578 filename: Specify the log file name, you can carry the specific path, When this parameter is not specified, the log output to the screen FileMode: Specifies the log write mode, the default is ' a ' for append, can be specified as ' W ' means overwrite ' logging.debug (' Debug message ') logging.info (' Info message ') logging.warning (' Warning message ') logging.error (' Error message ') logging.critical ( ' Critical message ')
The above example outputs the log to the Test.log file, which reads:
SAT,05 2018 22:25:14 2.py[line:22] Debug Debug messagesat,05 May 2018 22:25:14 2.py[line:23] Info info messagesat,05 M Ay 2018 22:25:14 2.py[line:24] WARNING WARNING messagesat,05 may 2018 22:25:14 2.py[line:25] Error Error messagesat,05 may 2018 22:25:14 2.py[line:26] CRITICAL CRITICAL message
If you only need the log output to the screen, simply comment out the two lines of filename= ' Test.log ' and filemode= ' a '.
2, Logging.getlogger Way
Full configuration, can be output to the screen or file separately, also can output simultaneously. For example:
#-*-coding:utf-8-*-#日志输出更灵活, you can export to screen and file import logging# separately or simultaneously create a Logger object Logger=logging.getlogger () # Creates a file output stream handler, which is used to write to the log file fm=logging. Filehandler (' Test1.log ') #再创建一个标准输出流handler for output to screen pm=logging. Streamhandler () #创建一个日志格式对象formatter =logging. Formatter ('% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ') #为两个handler添加日志格式fm. Setformatter ( Formatter) Pm.setformatter (formatter) #为logger对象添加handlerlogger. ADDHANDLER (FM) Logger.addhandler (PM) #设置输出日志级别, The default output is only warning above logger.setlevel (logging. Debug) logger.debug (' Debug Message ') Logger.info (' info message ') logger.warning (' warning message ') logger.error (' Error message ') logger.critical (' Critical message ')
The above example, the log output to the screen and the Test1.log file, the content:
2018-05-05 22:32:52,800 3.py[line:28] Debug debug message2018-05-05 22:32:52,800 3.py[line:29] Info info message2018-05-05 22:32:52,800 3.py[line:30] WARNING WARNING message2018-05-05 22:32:52,800 3.py[line:31] Error Error message2018-05-05 22:32:52,800 3.py[line:32] CRITICAL CRITICAL message
Logger.addhandler () determines the direction of the log output, and if it is output to only one aspect, the other line can be commented out.
Python notes-logging of built-in modules