This is the 22nd day of my participation in the August More Text Challenge

In the actual development process, logs are usually used to facilitate debugging programs, record the running status and errors of the system, and discover and locate system problems in a timely manner. Here’s how to use logging in Python projects.

Python provides a standard library module for logging, which is used to output formatted content to screens or files. The Logging module implements a number of powerful functions and classes that provide a flexible event logging system for applications.

Simple use of logging

The level of logging

The logging module has five log levels: DEBUG, INFO, WARNING, ERROR, and CRITICAL.

level The numerical Using range
CRITICAL 50 An irreversible error occurs and the system cannot work properly
ERROR 40 An error occurs in the program and some functions do not work properly
WARNING 30 A warning message is displayed and the system is still running normally
INFO 20 Usually only information about general events in the program is recorded to confirm that everything is working properly
DEBUG 10 It is used during function development to check whether the system functions are normal

Level sorting: DEBUG < INFO < WARNING < ERROR < CRITICAL. The lowest level is DEBUG, and the highest level is CRITICAL. If the log level is set, the logger will only process INFO, WARNING, ERROR, and CRITICAL logs when calling a logging function at a lower level, such as setLevel(logging.info). DEBUG messages are ignored.

Log output

import logging


logging.debug("This log is debug.")
logging.info("This log is info level.")
logging.warning("This log is of warning level")
logging.error("This log is at error level.")
logging.critical("This log is critical.")
Copy the code

The execution result is as follows:



This is because the default log level is ERROR. As stated above, calls to logging functions lower than the ERROR level do not output any messages, so logs lower than the ERROR level are not output. To change the log level for output, you can change the log level using the basicConfig() method of the logging module.

import logging


logging.basicConfig(level=logging.DEBUG)  # Change the log level to DEBUG
logging.debug("This log is debug.")
logging.info("This log is info level.")
logging.warning("This log is of warning level")
logging.error("This log is at error level.")
logging.critical("This log is critical.")
Copy the code

The execution result is as follows:

Set the log output format

Each line of log information in the preceding output is in the format of log level: logger name: log content. This is because the logging module uses the default log format BASIC_FORMAT for logging functions. The value is: %(LevelName)s:%(name)s:%(message)s You can customize the log output format as follows:

format instructions
%(levelno)s Log level value
%(levelname)s Name of a log level
%(pathname)s Path to the currently executing program
%(filename)s Name of the currently executing program
%(funcName)s The current function for logging
%(lineno)d Log is the forward number
%(asctime)s Log time
%(thread)d Thread ID
%(threadName)s Name of the thread
%(process)d The process ID
%(message)s Log information

The following is an example of setting the log output format:

import logging


log_formatter = '%(levelName)s %(asctime)s %(filename)s: lineno d line: %(message)s'
logging.basicConfig(level=logging.DEBUG, format=log_formatter)
logging.debug("This log is debug.")
logging.info("This log is info level.")
logging.warning("This log is of warning level")
logging.error("This log is at error level.")
logging.critical("This log is critical.")
Copy the code

The execution result is as follows:

Advanced usage of Logger

If you simply use the logging module, the above method will suffice, but the logging module can be used in more ways than that. See below. The Logging module adopts a modular design and consists of four components:

  • Loggers: Loggers that provide interfaces called directly by applications
  • Handlers: the Handlers that decide to allocate logging to the correct destination;
  • Filters: Provides better granularity control to filter log information and determine which logs will be output
  • Formatters: sets the output format of log records

The typical steps to use logging in practice are to create the logger, then set the log level, then create the log file, then set the log format, and finally log handlers to the logger.

Example:

import logging
from logging.handlers import RotatingFileHandler

Create logger
logger = logging.getLogger()
# Set the log level
logger.setLevel(logging.DEBUG)
Create a logger that specifies the path to save the log, the maximum size of each log file, and the maximum number of log files to save
file_log_handler = RotatingFileHandler("logs/test.log", maxBytes=1024 * 1024 * 100, backupCount=10)
Create a format for logging
formatter = logging.Formatter('%(levelName)s %(asctime)s %(filename)s: lineno d line: %(message)s')
Set the logging format
file_log_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()  # Output to screen
Add logger
logging.getLogger().addHandler(file_log_handler)
logging.getLogger().addHandler(stream_handler)
logging.debug("This log is debug.")
logging.info("This log is info level.")
logging.warning("This log is of warning level")
logging.error("This log is at error level.")
logging.critical("This log is critical.")
Copy the code

Using the example code above, we can output logs to a file, but if we want to output logs to both the file and the screen, we need to create and add a logging handler for the screen: logging.streamHandler ().

conclusion

Logging is an important part of programming. If you haven’t started using it yet, or if you’re still using print logging, get started.

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!