python logging module

Logging related knowledge

Log level scenes to be used
DEBUG Detailed information, generally only used when debugging problems
INFO Prove that things are working as expected
WARNING Some unexpected time prompts, or prompts for problems that may occur in the future
ERROR Due to more serious problems, the software can no longer perform some functions
CRITICAL A serious error indicates that the software can no longer run

Note:
Severity level: CRITICAL>ERROR>WARNING>INFO>DEBUG
Default level: WARNING
Set the output log level: If the log level is WARNING, the log can only print WARNING and above log content, and the severity level is less than WARING is the
default generation that is not printed.
The level of the root logger is logging.WARNING, and those below this level will not be output.

The Formatter object sets the final rules, structure and content of the log information

parameter Output content
%(name)s Logger's name
%(levelno)s Digital log level
%(message)s User output information
%(levelname)s Log level in text form
%(pathname)s The full path name of the module that calls the log output function, it may not
%(filename)s The file name of the module that calls the log output function
%(module)s The name of the module that calls the log output function
%(lineno)d The line of code where the statement that calls the log output function is located
%(asctime)s The current time as a string. The default format is "2003-07-08 16:49:45,896". Milliseconds after the comma
%(thread)d Thread ID, may not
%(threadName)s Thread name. Maybe not

logging to the console

import logging
logging.debug('it is debug')
logging.info('it is info')
logging.warning('it is waring')
logging.error('it is error')
logging.critical('it is critical')
输出只有:
WARNING:root:it is waring
ERROR:root:it is error
CRITICAL:root:it is critical
注释: 默认生成的root logger的level是logging.WARNING,低于该级别的就不输出了

logging output to file

import logging
#创建一个logger
logger = logging.getLogger()
#设置log等级开关
logger.setLevel(logging.INFO)
logfile = 'recorder.log'
fh = logging.FileHandler(logfile, mode='w', encoding='utf-8')
#设置输出格式
fh.setLevel(logging.DEBUG)
#设置格式
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
fh.setFormatter(formatter)
#将logger添加到hander里面
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')

Simple log decorator

import logging
import json

def create_log():
    logger = logging.getLogger()
    # 设置log等级开关
    logger.setLevel(logging.INFO)
    logfile = 'warrp.log'
    fh = logging.FileHandler(logfile, mode='w', encoding='utf-8')
    # 设置输出格式
    fh.setLevel(logging.DEBUG)
    # 设置格式
    formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    fh.setFormatter(formatter)
    # 将logger添加到hander里面
    logger.addHandler(fh)
    return logger


def write_log(func):
    def wrapper():
        result = func()
        logger = create_log()
        logger.info(json.dumps(result))
        return result
    return wrapper

@write_log
def func():
    return {
    
    'name': 'hh', 'age': 20}

if __name__ == '__main__':
	func()

Guess you like

Origin blog.csdn.net/xxy_yang/article/details/107913769