Article Directory
logging module learning
Log module:
Module for printing log information.
import logging
#测试日志信息
logging.debug("debug message");
#信息日志信息
logging.info("info message");
#警告日志信息
logging.warn("warn message");
#错误日志信息
logging.error("error message");
#严重错误日志信息
logging.critical("critical message");
Only the log information level is WARNING, the logging module will print the log to the screen. (That is, only logs with a log level higher than WARNING will be output)
Log level and settings
level | When to use |
---|---|
DEBUG | Detailed information, which is typically of interest when debugging problems. |
INFO | Prove that things are working as expected. |
WARNING | Indicates that some accident has occurred, or a problem will occur in the near future (such as'disk full'). The software is still working normally. |
ERROR | Due to more serious problems, the software can no longer perform some functions. |
CRITICAL | A serious error indicates that the software can no longer run. |
Simple configuration of the log:
import logging
#logging模块的基础设置,filename为日志打印文件(没有会创建一个文件),level为日志等级。
logging.basicConfig(filename="logger.log", level=logging.INFO)
#basicCondig的level设置的是logging的最大等级,也就是这里的等级大于INFO的都输出
logging.debug("debug message");
logging.info("info message");
logging.warn("warn message");
logging.error("error message");
logging.critical("critical message");
After the basic setting function is set, the log will not be displayed in the command box. Instead, it will be printed to a file.
Recorder, processor, filter, formatter
- logger: Recorder. Log information is generated, and the interface of the application code will be displayed when there is an error.
- handler: The processor. Send the log records generated by the processor to the appropriate destination.
- filter: filter. Filter log information.
- formatter: formatter. Specifies the log format.
The relationship between them is as follows:
Logger
The logger is a tree structure container. Before using the debug, warn, error, and critical interfaces, a Logger instance must be created. Otherwise, root logger is created, default: log level (warn), processor (StreamHandler), information is printed on the screen, formatter (default formatted output)
# 创建方法
logger = logging.getLogger(logger_name)
Modify recorder information:
- logger.setLevel(logging.ERROR): Set the processor log level.
- logger.addHandler(handler_name): Add a handler to the logger.
- logger.removeHandler(handler_name): Remove a handler in the logger.
Handler
Common processors are:
- StreamHandler
- FileHandler
StreamHandler
# 创建方法
streamHandler = logging.StreamHandler(stream=None)
There are the following methods:
- streamHandler.setLevel(logging.WARN): Specify the log level.
- streamHandler.setFormatter(formatter_name): Set a formatter.
- streamHandler.addFilter(filter_name): Add a filter.
- streamHandler.removeFiltert(filter_name): Remove a filter.
FileHandler
# 创建方法
fileHandler = logging.FileHandler(filename, mode="a", encoding=None, delay=False)
Formatter
The formatter regulates the format of the output log information. It specifies the rules, format, and content of log information.
# 创建方法
formatter = logging.Formatter(fmt=None, datefmt=None)
fmt is the format string message. datefmt is the format string of the date. The default fmt uses'%(message)s', and the default datefmt uses the ISO8601 date format.
Log configuration
-
Explicitly create Logger, Handler and Formatter, and make related settings;
-
Configure in a simple way, use the basicConfig() function to configure directly;
-
Configure through the configuration file, use the fileConfig() function to read the configuration file;
-
Configure through the configuration dictionary, use the dictConfig() function to read the configuration information;
-
Configure through the network, use the listen() function to configure the network.
BasicConfig parameter information:
Keyword | description |
---|---|
filename | Create a FileHandler and use the specified file name instead of StreamHandler. |
filemode | If the file name is specified, specify the mode of opening the file (if filemode is not specified, the default is'a'). |
format | The handler uses the specified format string. |
datefmt | Use the specified date/time format. |
level | Specify the level of the root logger. |
stream | Use the specified stream to initialize the StreamHandler. This parameter is incompatible with'filename'. If both are present,'stream' is ignored. |
Useful format
format | description |
---|---|
%(levelno)s | Print log level value |
%(levelname)s | Print log level name |
%(pathname)s | Print the path of the currently executing program |
%(filename)s | Print the name of the currently executing program |
%(funcName)s | Current function for printing log |
%(lineno)d | Print the current line number of the log |
%(asctime)s | Time to print the log |
%(thread)d | Print thread id |
%(threadName)s | Print thread name |
%(process)d | Print process ID |
%(message)s | Print log information |