Popular on the whole network, how to use python automated testing logging module (details)


foreword

1. Log level

import logging  # 引入logging模块


# 将信息打印到控制台上
logging.debug(u"勇士")
logging.info(u"湖人")
logging.warning(u"太阳")
logging.error(u"雄鹿")
logging.critical(u"热火")

The level of the root logger generated by default is logging.WARNING, and those below this level will not be output

Level sorting: CRITICAL > ERROR > WARNING > INFO > DEBUG

debug : Print all logs, detailed information, usually only for diagnosing problems
info : Print info, warning, error, critical level logs to confirm that everything works as expected
warning : Print warning, error, critical level logs, one Indications that something unexpected has happened, or that some problem is in the near future (eg. low disk space"), the software still works as expected error
: print error, critical level log, more serious problems, The software failed to perform some function
critical: prints a critical level, a critical error, which indicates that the program itself may not continue to run

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

import logging  # 引入logging模块


logging.basicConfig(level=logging.NOTSET)  # 设置日志级别
logging.debug(u"如果设置了日志级别为NOTSET,那么这里可以采取debug、info的级别的内容也可以显示在控制台上了")

2. Analysis and explanation

Logging.Formatter: This class configures the format of the log, in which the date and time are customized, and when the log is output, the content will be displayed according to the format set.

Logging.Logger: Logger is the main body of the Logging module.
Perform the following three tasks:
provide an interface for the program to record logs;
determine the level of the log and determine whether to filter it;
distribute the log to different handlers according to its log level;

Commonly used functions are:
Logger.setLevel() to set the log level;
Logger.addHandler() and Logger.removeHandler() to add and delete a Handler;
Logger.addFilter() to add a Filter for filtering purposes;
Logging.Handler: Handler is based on the log level Distribute the logs. For example,
the Handler set to the WARNING level will only process the logs of the WARNING level and above.

Commonly used functions are:
setLevel() to set the level;
setFormatter() to set the Formatter;

3. Log output - console

import logging  # 引入logging模块


logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')  # logging.basicConfig函数对日志的输出格式及方式做相关配置
# 由于日志基本配置中级别设置为DEBUG,所以一下打印信息将会全部显示在控制台上
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, all information above the DEBUG level will be output and displayed on the console.

4. Log output - file

import logging  # 引入logging模块
import os.path
import time


# 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log等级总开关
# 第二步,创建一个handler,用于写入日志文件
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)  # 输出到file的log等级的开关
# 第三步,定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 第四步,将logger添加到handler里面
logger.addHandler(fh)
# 日志
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')

5. Log output - console and file

Just insert a handler output to the console in the second and third steps of the input to the log:
create a handler for output to the console

ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)  # 输出到console的log等级的开关

Steps 4 and 5 add the following codes respectively

ch.setFormatter(formatter)
logger.addHandler(ch)

6. Common format description of format

%(levelno)s: Print the value of the log level
%(levelname)s: Print the name of the log level
%(pathname)s: Print the path of the current execution program, which is actually sys.argv[0]
%(filename)s: Print the current Execution program name
%(funcName)s: print the current function of the log
%(lineno)d: print the current line number of the log
%(asctime)s: print the time of the log
%(thread)d: print the thread ID
%(threadName)s : Print thread name
%(process)d: Print process ID
%(message)s: Print log information

7. Capture exception traceback records

import os.path
import time
import logging


# 创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log等级总开关

# 创建一个handler,用于写入日志文件
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)  # 输出到file的log等级的开关

# 定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
logger.addHandler(fh)
# 使用logger.XX来记录错误,这里的"error"可以根据所需要的级别进行修改
try:
    open('/path/to/does/not/exist', 'rb')
except (SystemExit, KeyboardInterrupt):
    raise
except Exception, e:
    logger.error('Failed to open file', exc_info=True)

If you need to not report errors in the log, but only record, you can write exc_info=False

8. Multi-module call logging, log output sequence

warning_output.py

import logging


def write_warning():
    logging.warning(u"记录文件warning_output.py的日志")

error_output.py

import logging


def write_error():
    logging.error(u"记录文件error_output.py的日志")

main.py

import logging
import warning_output
import error_output


def write_critical():
    logging.critical(u"记录文件main.py的日志")


warning_output.write_warning()  # 调用warning_output文件中write_warning方法
write_critical()
error_output.write_error()  # 调用error_output文件中write_error方法

9. Log rolling and expired deletion (according to time)

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


def backroll():
    #日志打印格式
    log_fmt = '%(asctime)s\tFile \"%(filename)s\",line %(lineno)s\t%(levelname)s: %(message)s'
    formatter = logging.Formatter(log_fmt)
    #创建TimedRotatingFileHandler对象
    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)
    #循环打印日志
    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()

Description:
filename: the prefix of the log file name;
when: a character string used to describe the basic unit of the rolling period, the value and meaning of the character 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 when, for example: when='D', interval=1, which means that a log is generated every day File
backupCount: Indicates the number of retained log files

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Life is short, let's live our best life. Every day is a new beginning. No matter how many setbacks and difficulties you encounter, you must persevere in your struggle and believe that your efforts will eventually pay off. As long as you maintain a positive attitude and perseverance, the pursuit of your dreams will surely come true.

Our time is limited, don't waste it living your life for others. Embrace challenges, surpass yourself, and strive for the life you want! Only with unremitting efforts can we realize our dreams. Believe in yourself, believe in your ability, you can do it!

Only by constantly striving can we welcome a better future; success requires courage, perseverance and patience, unremitting efforts will eventually yield results; every day is a new beginning, we must strengthen our beliefs, go forward bravely, and continue to pursue our dreams.

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/131288437