Commonly used treasure chest - log processing

Table of contents

Preface

1. Logging library

2. Logging level

3. Four major components of logging

4. Packaging example

Summarize


Preface

        A log is a file or record that records details of a specific time period or event. They identify events or actions through timestamps and keywords or descriptors. Logs can be used for many purposes such as: troubleshooting, network security, business analysis, and more. Common logs include operating system logs, application logs, database logs, etc. Logs can be analyzed and summarized through software tools to help users better understand the operation and performance of the system, and discover possible problems and anomalies.

1. Logging library

The logging library is Python's official log processing module. It provides a flexible, simple but powerful way to record application log information. You can use the logging library to collect, filter, format, and output control the log information of the application.

The following functions can be achieved through the logging library:

  1. Record application log information to a file or console.

  2. Filter the output log information based on the log level.

  3. Log information in different formats is output according to different configurations.

  4. Automatically rotate log files according to configuration to prevent log files from becoming too large.

  5. Supports multi-threaded and multi-process applications.

  6. Log processors can be customized to meet specific log processing requirements.

2. Logging level

The logging level is part of the Python standard library and is used to log and output information in programs. The log level describes the importance of logs and is used to filter and control log output. The following are the logging levels in the Python logging library (in order from lowest to highest):

logging level
Log level         describe
DEBUG Detailed information for debugging the program
INFO General information used to describe the running status of the program
WARNING Warning message, indicating that there may be a problem with the program but it is not serious
ERROR Error message indicating that the program has encountered an unrecoverable error
CRITICAL A serious error message indicating that the program has encountered a fatal error

Normally, you can use the INFO log level, but when you encounter debugging problems, you can use DEBUG for detailed tracing. Warnings, errors, and serious error messages need to be paid attention to and processed in a timely manner. You can filter the required log output by setting log levels and filters.

3. Four major components of logging

  1. logger: used to record events that occur in the application, such as information, errors, and warnings. They are usually divided into different levels according to the severity level, such as debug, info, warning, error and critical.

  2. Handler: Used to control when and how logged events are sent to the target location, such as the console, file, database, or network.

  3. formatter: used to specify the format of log records, including timestamp, level, message, etc.

  4. filter: used to control which log records will be recorded and which ones will be ignored. You can filter based on conditions such as log level, message content and source.

4. Packaging example

The following is an example of simply using the logging library to output logs:

import logging

# 创建一个日志对象
logger = logging.getLogger(__name__)

# 设置日志级别
logger.setLevel(logging.INFO)

# 创建一个输出到控制台的处理器
console_handler = logging.StreamHandler()

# 设置日志格式
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
console_handler.setFormatter(formatter)

# 添加处理器到日志对象中
logger.addHandler(console_handler)

# 输出日志
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')

In the above sample code, a log object is first created and the log output level is set to INFO. Then a processor is created that outputs to the console, and the log output format is set. Finally add a processor to the log object and output the log.

Running the above code, you can see that different levels of log information are output. Because the output level is set to INFO, only log information of INFO and above levels are output, and log information of DEBUG and WARNING levels are not output.

Summarize

In addition to the logging library, Python also has the following libraries for logging:

  1. Logbook: a flexible, powerful and easy-to-use log library;

  2. Loguru: an easy-to-use logging library that supports colors and optional Stack Trace;

  3. Sentry: a full-featured logging, error collection, real-time event monitoring and alerting platform;

  4. Python's own syslog module: can send log information to the local syslog daemon;

  5. Graylog: an open source log management platform that supports multiple input and output methods.

These libraries can provide different levels of logging functions, and you need to choose according to actual needs when using them.

6adf31c8c5dd4e6a83314f4805b30bc1.jpg

Guess you like

Origin blog.csdn.net/BROKEN__Y/article/details/132842213