Learning notes - python import module

import logging module

Reprint: https://mp.weixin.qq.com/s/K3k-Mk75tSe-Z8cTooyJTg
https://blog.csdn.net/zywvvd/article/details/87857816
The simplest way to record a log is to print at the place to be recorded, but this method has no time to print, does not know the location of the log record, and has no readable log format. Therefore, it is necessary to use the python built-in logging module.
1.logging framework
It mainly includes four parts:
(1) Logger
Returns the logger with the specified name or, if no name is specified, the root logger of the hierarchy
(2) Handler (processor)
Accurately distribute the information sent by the logger and send it to the correct place (such as console, file, etc.)
Python has built-in many practical processors, including:
1. StreamHandler standard stream processor, which sends messages to standard output stream and error stream
2. FileHandler is a file processor that sends messages to files
3. The RotatingFileHandler file processor enables a new file to store logs after the file reaches the specified size
4. TimedRotatingFileHandler is a file processor that rotates log files at specific time intervals
(3) Formatter (formatter)
Specify the format and layout of a final record print, and splice the transmitted data into a specific string.
There are some built-in LogRecord properties in Format that can be used, as shown in the following table:

(4) Filter
Provide finer grained judgment to determine whether the log needs to be printed. For example, Filter can intercept or modify logs from specific sources, and even modify its log level (judge the level after modification).
2. Log level

3. Common functions
(1)logging.basicConfig()
Basic configuration of the log system. This function does nothing if the root logger has a handler configured. Keyword parameters:
filename: save path of log file. If some parameters are configured, a FileHandler will be automatically created as the Handler;
File mode: the open mode of the log file. The default value is' a ', which means that the log message is added to the log file in the form of append. If it is set to 'w', a new log file will be created every time the program starts;
Format: set the log output format;
datefmt: defines the date format;
Level: sets the level of the log Log messages below this level will be ignored;
Stream: set a specific stream to initialize StreamHandler;
(2)logging.getLogger()
Returns the root logger in the log object hierarchy.
(3)logging.getLevelName()
Gets the name corresponding to the log level.
(4)logger.setLevel():
Set the level of the log. Log messages below this level will be ignored.
4. Basic use
(1)

import logging
logging.basicConfig(level=logging.DEBUG)#Modify log default level
logging.debug("this is debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
'''Print results
DEBUG:root:this is debug
INFO:root:this is info
WARNING:root:this is warning
ERROR:root:this is error
CRITICAL:root:this is critical
'''
import logging
logging.basicConfig()
logging.debug("this is debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
'''Print results
WARNING:root:this is warning
ERROR:root:this is error
CRITICAL:root:this is critical
 The default log level for basic configuration is WARGING´╝îlower than WARING No log information will be output
'''

(2) Specify log format

import logging
logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s %(message)s')
logging.error("this is error")
'''Print results
2021-12-23 16:44:21,547 ERROR root this is error
'''

Keywords: Python Back-end

Added by ironman on Sat, 25 Dec 2021 23:46:30 +0200