Python automation framework construction - log collection

1. Log concept

        The log is used to record the information when the system is running, and the record of an event is also called Log.

2. Log function

  • Debugger
  • Understand the running situation of the system program, whether it is normal
  • System program operation failure analysis and problem location
  • Used for user behavior analysis and data statistics

3. Log level

Log level: refers to the priority, importance or severity of log information

Common log levels describe
DEBUG Debug level, print very detailed log information, usually used for code debugging

INFO

Information level, print general log information, highlight the running process of the program
WARNING Warning level, print warning log information, indicating that there will be potential errors, generally does not affect the normal use of the software
ERROR Error level, print error exception information, errors of this level may cause some functions of the system to be unable to use normally
CRITICAL Severe error level, a serious error, which indicates that the system may not continue to operate

illustrate:

        The log level in the above list increases from top to bottom, namely: DEBUG < INFO < WARNING < ERROR < CRITICAL;        

        When a log level is specified for the program, the program will record all log information whose log level is greater than or equal to the specified log level, instead of only recording the log information of the specified level;        

        It is generally recommended to only use the four levels of DEBUG, INFO, WARNING, and ERROR.

4. Basic usage of logs

import logging    # 导入日志依赖包

logging.debug("这是一条调试信息") 
logging.info("这是一条普通信息") 
logging.warning("这是一条警告信息") 
logging.error("这是一条错误信息") 
logging.critical("这是一条严重错误信息")

(1) Set the log level

logging.basicConfig(level=logging.DEBUG)

level可选:
    logging.DEBUG
    logging.INFO
    logging.WARNING
    logging.ERROR
    logging.CRITICAL

Note: The default log level in logging is WARNING. Only logs greater than or equal to this level in the program can be output, and logs less than this level will not be printed.

(2) Set the log format

The default log format is: log level: Logger name: log content

Custom log format:

logging.basicConfig(format="格式化信息")

常用的格式化信息
format = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'

Formatting information that may be used by the format parameter:

Placeholder describe

%(name)s

Logger's name
%(levelno)s log level in numeric form
%(levelname)s log level as text
%(pathname)s The full pathname of the module that called the log output function, possibly without
%(filename)s The filename of the module that called the log output function
%(module)s The module name that calls the log output function
%(funcName)s The function name that calls the log output function
% (linen) d The line of code where the statement that calls the log output function is located
%(created)f The current time, represented by a UNIX standard floating-point number representing the time
%(relativeCreated)d When outputting log information, the number of milliseconds since the Logger was created
%(asctime)s The current time as a string. The default format is "2003-07-08 16:49:45,896"
%(thread)d thread ID. Maybe not
%(threadName)s thread name. Maybe not
%(process)d process ID. Maybe not
%(message)s user output message

(3) Output log information to a file

By default, Python's logging module prints logs to standard output (console).

Output log information to a file:

logging.basicConfig(filename="输出的文件名.log")

Advanced usage of logs

1. Four major components of the logging module

component name class name Functional description
logger Logger Provides the entry of the program usage log
processor Handler Send log records created by the logger to the appropriate destination output
formatter Formatter Determines the final output format of the logging
filter Filter Provides finer-grained control tools to decide which log records to output and which log records to discard

2. The relationship between components

Logger: Provides an entry for recording logs, such as: log.info("")
Processor: It is the processor that actually sends the contents of the logger to the console or to a file or network; each logger can be added Multiple different processors
Formatter: The processor can set different formats, you need to use the formatter
Filter: The processor needs to filter log information, you need to set the filter

3. Logger class

The tasks of the Logger class object:

  • Expose logging methods to programs
  • Based on the log level or Filter object to decide which logs to follow up
  • Deliver log messages to all interested log handlers

How to create a Logger class object:

logger = logging.getLogger() 

或者

logger = logging.getLogger(name)

The logging.getLogger() method has an optional parameter name, which indicates the name of the logger to be returned. If this parameter is not provided, the root logger object will be returned. If the getLogger() method is called multiple times with the same name parameter value, a reference to the same logger object will be returned

Commonly used methods of Logger:

method describe

logger.debug()

logger.info()

logger.warning()

logger.error()

logger.critical()

print log
logger.setLevel() Sets the minimum severity level of log messages that the logger will process
logger.addHandler() Add a handler object to the logger object
logger.addFilter() Add a filter object to the logger object

4. Handler class

Function: The function of the Handler object is to distribute the message to the location specified by the handler, such as: console, file, network, mail, etc. The Logger object can add multiple handler objects for itself through the addHandler() method.

How to create a Handler class object:

Handler describe
logging.StreamHandler Send log messages to output to Stream, such as std.out, std.err or any file-like object
logging.FileHandler Send log messages to a disk file, which grows infinitely in size by default
logging.handlers.RotatingFileHandler Send log messages to disk files, and support log files to be cut by size
logging.hanlders.TimedRotatingFileHandler Send log messages to disk files, and support log files to be cut by time
logging.handlers.HTTPHandler Send log messages to an HTTP server as GET or POST
logging.handlers.SMTPHandler Send log messages to a specified email address

Commonly used methods of Handler:

method describe
handler.setLevel() 设置handler将会处理的日志消息的最低严重级别
handler.setFormatter() 为handler设置一个格式器对象
handler.addFilter() 为handler添加一个过滤器对象

5、Formatter类

作用:Formatter对象用于配置日志信息的格式。

如何创建Formatter类对象:

formatter = logging.Formatter(fmt=None, datefmt=None, style='%') 
    fmt:指定消息格式化字符串,如果不指定该参数则默认使用message的原始值 
    datefmt:指定日期格式字符串,如果不指定该参数则默认使用"%Y-%m-%d %H:%M:%S" 
    style:Python 3.2新增的参数,可取值为 '%', '{'和 '$',如果不指定该参数则默认使用'%'

6、日志高级用法综合案例分析

目标:将日志信息同时输出到控制台和文件中

import logging.handlers    # 导入此包才能使用handler类创建对象

# 获取logger
logger = logging.getLogger()

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

# 获取控制台 处理器
sh = logging.StreamHandler()
# 根据时间切割文件 处理器
th = logging.handlers.TimedRotatingFileHandler(filename="文件名.log",
                                                when="S",
                                                interval=1,
                                                backupCount=3,
                                                encoding="utf-8")

# 添加格式器
fmt = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s (%(funcName)s:%(lineno)d] - %(message)s"
fm = logging.Formatter(fmt)

# 将格式器 添加到处理器中
sh.setFormatter(fm)
th.setFormatter(fm)


# 将 控制台处理器添加到 logger
logger.addHandler(sh)
logger.addHandler(th)

# 打印信息
logger.info("info")
logger.debug("debug")
logger.error("error")
logger.warning("warning")

logging.handlers.TimedRotatingFileHandler()参数说明:

filename:文件名
when:间间隔的类型

      可选S(秒)、M(分钟)、H(小时)、D(天)、midnight(一夜)、W{0-6}(周几)
interval: 时间间隔
backupCount:能留几个日志文件,超过数量就会丢弃掉老的日志文件

Guess you like

Origin blog.csdn.net/ouihsiad/article/details/127132546