logging module
The role of the log
In general, acts as the role of print in the journal, some of the information display program to run with them, such as the current number of iterations and so on, but sometimes we make the program running in the background, then print function will be out of action, we need to information stored in the file to run, the longest in nohup.out to see a linux file, and afterwards we can view some information from programs running in the log file
logging module log level
From low to high:
* the DEBUG
* the INFO
* the WARNING
* ERROR
* CRITICAL in
when the program specifies a logging level, the program will record the log level greater than or equal to the specified log information
Common logging function module
longing.debug(msg, *args, **kwargs) # 创建一个等级为DEBUG的日志记录
将上面的debug替换成info, warning, error, critical就是创建一条严重级别为info, warning, error, critical的日志记录
logging.basicConfig(**kwargs) # 对日志记录器进行配置,如输出日志格式,日志等级等信息
logging of use
1. Only level is greater than the specified level equal to the logger will log the other output
import logging
logging.debug("hello word, debug")
logging.info("hello word, info")
logging.error("hello word, error")
logging.warning("hello word, warning")
logging.critical("hello word, critical")
Export
ERROR:root:hello word, error
WARNING:root:hello word, warning
CRITICAL:root:hello word, critical
[Finished in 14.5s]
Can be found in the front two logs is not output, because the default logging level is set for the recording WARNING, so only level greater than or equal WARNING log is output
2. modify the default configuration
used to modify the default configuration basicConfig, longging.basicConfig()
received the following parameters:
filename
file name of the log is written
filemode
to specify the log file open mode, the default is "a", is to append mode
format
specifying information contained in the log output order, will be described below custom format field
datefmt
specifies the date and time format
Here are several format string field frequently used
%(asctime)s
time diaries occurred
%(levelname)s
designated log level
%(message)s
logging content
3. After the log output configuration
procedure is as follows
import logging
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
logging.basicConfig(filename='./nohup.out', level=logging.DEBUG, format=LOG_FORMAT)
logging.debug("hello word, debug")
logging.info("hello word, info")
logging.error("hello word, error")
logging.warning("hello word, warning")
logging.critical("hello word, critical")
Output: At this time, since the output file is specified, the console does not display output, the log information recorded in the log file, wherein the file open as follows nohup.out
2020-01-10 21:32:03,006 DEBUG hello word, debug
2020-01-10 21:32:03,006 INFO hello word, info
2020-01-10 21:32:03,006 ERROR hello word, error
2020-01-10 21:32:03,006 WARNING hello word, warning
2020-01-10 21:32:03,006 CRITICAL hello word, critical
Because the logging level set level = logging.DEBUG, and therefore will not less than grade logs are recorded DEBUG