Detailed explanation of logging module in python

When writing code in Python, write a print xx where you want to see it, and you can display the print information on the console. In this way, you can know what it is. However, when I need to see a large number of places or view in a file, print is not convenient, so Python introduces the logging module to record the information I want.

Print can also input logs. Compared with print, logging better controls where to output, how to output and control the message level to filter out unwanted information.

1. Log level

import logging  # Introducing the logging module
# Print information to the console
logging.debug("Sora Aoi")
logging.info("Ma Shengxi")
logging.warning("Maria Ozawa")
logging.error("Peach Valley thyme")
logging.critical("Rola Takizawa")

Echo:

As you can see, only the last three can be printed

The level of the root logger generated by default is logging.WARNING. If it is lower than this level, it will not be output
Level sorting: critical > error > warning > info > debug

debug: print all logs and detailed information, which usually only appears in the diagnosis of problems

Info: print logs at info, warning, error and critical levels to confirm that everything runs as expected

Warning: print the warning,error,critical level log. It is an indication that something unexpected has happened or that some problems will occur in the near future (for example, low disk space). This software can still work as expected

Error: print error,critical level log, more serious problem, the software fails to perform some functions

Critical: print the critical level, a serious error, indicating that the program itself may not continue to run

At this time, if you need to display content lower than WARNING level, you can introduce NOTSET level to display:

import logging  # Introducing the logging module
logging.basicConfig(level=logging.NOTSET)  # Set log level
logging.debug(u"If the log level is set to NOTSET,So what can be done here debug,info The level of content can also be displayed on the console")

Echo:

2. Interpretation of some terms

Logging.Formatter: this class configures the log format, where you can customize the date and time. When outputting the log, the content will be displayed according to the set format.
Logging.Logger: the logger is the main body of the logging module and performs the following three tasks:

  1. Provide an interface for the program to record logs
  2. Judge the level of the log and whether to filter it
  3. Distribute the log to different handler s according to its log level
    Common functions are:
    Logger.setLevel() sets the log level
    Logger.addHandler() and Logger.removeHandler() add and remove a Handler
    Logger.addFilter() adds a Filter to Filter
    Logging.Handler: the handler distributes logs based on the log level. For example, the handler set to WARNING level will only process logs of WARNING level and above.
    Common functions are:
    setLevel() sets the level
    setFormatter() sets the Formatter

3. Log output - console

import logging  # Introducing the logging module
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')  # The logging.basicConfig function configures the log output format and method
# Since the level in the basic log configuration is set to DEBUG, all the next printing information will be displayed on the console
logging.info('this is a loggging info message')
logging.debug('this is a loggging debug message')
logging.warning('this is loggging a warning message')
logging.error('this is an loggging error message')
logging.critical('this is a loggging critical message')

The above code configures the log level and log content output format through the logging.basicConfig function; Because the level is DEBUG, the information above DEBUG level will be output and displayed on the console.

Echo:

4. Log output - file

import logging  # Introducing the logging module
import os.path
import time
# The first step is to create a logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log level master switch
# The second step is to create a handler for writing log files
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd()) + '/Logs/'
log_name = log_path + rq + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)  # Switch of log level output to file
# Step 3: define the output format of the handler
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# Step 4: add the logger to the handler
logger.addHandler(fh)
# journal
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message')

Echo (open files generated in the same directory):

5. Log output - console and files

Just insert one in the second and third steps entered into the log handler Output to console:
Create a handler´╝îFor output to console
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)  # Switch of log level output to console
 Add the following codes in step 4 and step 5 respectively
ch.setFormatter(formatter)
logger.addHandler(ch)
6,format Common format description
%(levelno)s: Print log level values
%(levelname)s: Print log level name
%(pathname)s: Print the path of the current executing program, which is actually sys.argv[0]
%(filename)s: Print the name of the currently executing program
%(funcName)s: Current function for printing logs
%(lineno)d: Print the current line number of the log
%(asctime)s: Time to print log
%(thread)d: Print thread ID
%(threadName)s: Print thread name
%(process)d: Printing process ID
%(message)s: Print log information

7. Catch exceptions and record with traceback

import os.path
import time
import logging
# Create a logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log level master switch

# Create a handler to write to the log file
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd()) + '/Logs/'
log_name = log_path + rq + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)  # Switch of log level output to file

# Define the output format of the handler
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
logger.addHandler(fh)
# Use logger.XX to record errors. The "error" here can be modified according to the required level
try:
    open('/path/to/does/not/exist', 'rb')
except (SystemExit, KeyboardInterrupt):
    raise
except Exception, e:
    logger.error('Failed to open file', exc_info=True)

Echo (stored in file):

If you need to record the log without reporting errors, you can set exc_info=False, echo as follows:

8. Multiple modules call logging, and the log output sequence

warning_output.py

import logging


def write_warning():
    logging.warning(u"Record file warning_output.py Log of")

error_output.py

import logging


def write_error():
    logging.error(u"Record file error_output.py Log of")

main.py

import logging
import warning_output
import error_output


def write_critical():
    logging.critical(u"Record file main.py Log of")


warning_output.write_warning()  # Call warning_ Write in output file_ Warning method
write_critical()
error_output.write_error()  # Call error_ Write in output file_ Error method

Echo:

From the above, the log output order is consistent with the module execution order.

9. Log rollover and expiration deletion by time

# coding:utf-8
import logging
import time
import re
from logging.handlers import TimedRotatingFileHandler
from logging.handlers import RotatingFileHandler


def backroll():
    #Log print format
    log_fmt = '%(asctime)s\tFile \"%(filename)s\",line %(lineno)s\t%(levelname)s: %(message)s'
    formatter = logging.Formatter(log_fmt)
    #Create TimedRotatingFileHandler object
    log_file_handler = TimedRotatingFileHandler(filename="ds_update", when="M", interval=2, backupCount=2)
    #log_file_handler.suffix = "%Y-%m-%d_%H-%M.log"
    #log_file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}.log$")
    log_file_handler.setFormatter(formatter)
    logging.basicConfig(level=logging.INFO)
    log = logging.getLogger()
    log.addHandler(log_file_handler)
    #Cycle print log
    log_content = "test log"
    count = 0
    while count < 30:
        log.error(log_content)
        time.sleep(20)
        count = count + 1
    log.removeHandler(log_file_handler)


if __name__ == "__main__":
    backroll()

filename: prefix of log file name;

when: is a string used to describe the basic unit of rolling period. The value and meaning of the string are as follows:
"S": Seconds
"M": Minutes
"H": Hours
"D": Days
"W": Week day (0=Monday)
"midnight": Roll over at midnight

Interval: rolling period, the unit is specified by when, for example: when ='D ', interval=1, which means that a log file is generated every day

backupCount: indicates the number of reserved log files

Tags: Python

Posted on Thu, 11 Nov 2021 00:33:49 -0500 by kurdishvoice