Home  >  Article  >  Backend Development  >  How to use the logging module in Python

How to use the logging module in Python

WBOY
WBOYforward
2023-05-12 18:10:061610browse
    ##1. Low-configuration logging

    The logs are divided into the following five levels. These five levels match debug from bottom to top--> info-->warning-->error-->critical, the default lowest level is warning level.

    1.v1

    import logging
    
    logging.debug('调试信息')
    logging.info('正常信息')
    logging.warning('警告信息')
    logging.error('报错信息')
    logging.critical('严重错误信息')

    WARNING:root:warning message

    ERROR:root:error message
    CRITICAL:root:serious error message

    The v1 version cannot specify the log level; the log format cannot be specified; it can only print to the screen and cannot write to files. Therefore it can be changed to the following code.

    2.v2

    import logging
    
    # 日志的基本配置
    
    logging.basicConfig(filename='access.log',
                        format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S %p',
                        level=10)
    
    logging.debug('调试信息')  # 10
    logging.info('正常信息')  # 20
    logging.warning('警告信息')  # 30
    logging.error('报错信息')  # 40
    logging.critical('严重错误信息')  # 50

    You can change the default behavior of the logging module through specific parameters in the logging.basicConfig() function. The available parameters are:

    • filename: Create a FileHandler with the specified file name (the concept of handler will be explained in detail later), so that the log will be stored in the specified file.

    • filemode: File opening mode, this parameter is used when filename is specified. The default value is "a" and can also be specified as "w".

    • format: Specify the log display format used by the handler.

    • datefmt: Specify the date and time format.

    • level: Set the log level of the rootlogger (the specific concept will be explained later)

    • stream: Create a StreamHandler with the specified stream. You can specify the output to sys.stderr, sys.stdout or a file. The default is sys.stderr. If both filename and stream parameters are listed, the stream parameter will be ignored.

    Format string that may be used in the format parameter:

    • %(name)s Logger’s name

    • %(levelno)s Log level in numeric form

    • %(levelname)s Log level in text form

    • % (pathname)s The full path name of the module that calls the log output function, may not be

    • %(filename)s The file name of the module that calls 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

    • %(lineno)d The line of code where the statement that calls the log output function is located

    • %(created)f The current time, expressed as a UNIX standard floating point number representing time

    • %(relativeCreated)d The number of milliseconds since the Logger was created when outputting log information

    • %(asctime)s The current value in string form time. The default format is "2003-07-08 16:49:45,896". What follows the comma is the millisecond

    • %(thread)d thread ID. There may be no

    • %(threadName)s thread names. There may be no

    • %(process)d process ID. There may not be

    • %(message)s messages output by the user.

    v2 version cannot specify character encoding; it can only be printed to a file.

    3.v3

    The logging module contains four roles: logger, Filter, Formatter object, Handler

    • logger: the object that generates logs

    • Filter: object for filtering logs

    • Formatter object: You can customize different log format objects and then bind them to different Handler objects to use This is used to control the log formats of different Handlers

    • Handler: receives logs and controls printing to different places, FileHandler is used to print to files, StreamHandler is used to print to the terminal

    • '''
      critical=50
      error =40
      warning =30
      info = 20
      debug =10
      '''
      
      
      import logging
      
      # 1、logger对象:负责产生日志,然后交给Filter过滤,然后交给不同的Handler输出
      logger = logging.getLogger(__file__)
      
      # 2、Filter对象:不常用,略
      
      # 3、Handler对象:接收logger传来的日志,然后控制输出
      h2 = logging.FileHandler('t1.log')  # 打印到文件
      h3 = logging.FileHandler('t2.log')  # 打印到文件
      sm = logging.StreamHandler()  # 打印到终端
      
      # 4、Formatter对象:日志格式
      formmater1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                                     datefmt='%Y-%m-%d %H:%M:%S %p',)
      
      formmater2 = logging.Formatter('%(asctime)s :  %(message)s',
                                     datefmt='%Y-%m-%d %H:%M:%S %p',)
      
      formmater3 = logging.Formatter('%(name)s %(message)s',)
      
      
      # 5、为Handler对象绑定格式
      h2.setFormatter(formmater1)
      h3.setFormatter(formmater2)
      sm.setFormatter(formmater3)
      
      # 6、将Handler添加给logger并设置日志级别
      logger.addHandler(h2)
      logger.addHandler(h3)
      logger.addHandler(sm)
      
      # 设置日志级别,可以在两个关卡进行设置logger与handler
      # logger是第一级过滤,然后才能到handler
      logger.setLevel(30)
      h2.setLevel(10)
      h3.setLevel(10)
      sm.setLevel(10)
      
      # 7、测试
      logger.debug('debug')
      logger.info('info')
      logger.warning('warning')
      logger.error('error')
      logger.critical('critical')
    2. High configuration logging

    1. Configuration log file

    The above three versions of logs are just to lead to our following log configuration file

    import os
    import logging.config
    
    # 定义三种日志输出格式 开始
    standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
                      '[%(levelname)s][%(message)s]'  # 其中name为getLogger()指定的名字;lineno为调用日志输出函数的语句所在的代码行
    simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
    id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
    # 定义日志输出格式 结束
    
    logfile_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # log文件的目录,需要自定义文件路径 # atm
    logfile_dir = os.path.join(logfile_dir, 'log')  # C:\Users\oldboy\Desktop\atm\log
    
    logfile_name = 'log.log'  # log文件名,需要自定义路径名
    
    # 如果不存在定义的日志目录就创建一个
    if not os.path.isdir(logfile_dir):  # C:\Users\oldboy\Desktop\atm\log
        os.mkdir(logfile_dir)
    
    # log文件的全路径
    logfile_path = os.path.join(logfile_dir, logfile_name)  # C:\Users\oldboy\Desktop\atm\log\log.log
    # 定义日志路径 结束
    
    # log配置字典
    LOGGING_DIC = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': standard_format
            },
            'simple': {
                'format': simple_format
            },
        },
        'filters': {},  # filter可以不定义
        'handlers': {
            # 打印到终端的日志
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',  # 打印到屏幕
                'formatter': 'simple'
            },
            # 打印到文件的日志,收集info及以上的日志
            'default': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
                'formatter': 'standard',
                'filename': logfile_path,  # 日志文件
                'maxBytes': 1024 * 1024 * 5,  # 日志大小 5M  (*****)
                'backupCount': 5,
                'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
            },
        },
        'loggers': {
            # logging.getLogger(__name__)拿到的logger配置。如果''设置为固定值logger1,则下次导入必须设置成logging.getLogger('logger1')
            '': {
                # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
                'handlers': ['default', 'console'],
                'level': 'DEBUG',
                'propagate': False,  # 向上(更高level的logger)传递
            },
        },
    }
    
    
    
    def load_my_logging_cfg():
        logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
        logger = logging.getLogger(__name__)  # 生成一个log实例
        logger.info('It works!')  # 记录该文件的运行状态
        
        return logger
    
    
    if __name__ == '__main__':
        load_my_logging_cfg()

    2. Usage log

    import time
    import logging
    import my_logging  # 导入自定义的logging配置
    
    logger = logging.getLogger(__name__)  # 生成logger实例
    
    
    def demo():
        logger.debug("start range... time:{}".format(time.time()))
        logger.info("中文测试开始。。。")
        for i in range(10):
            logger.debug("i:{}".format(i))
            time.sleep(0.2)
        else:
            logger.debug("over range... time:{}".format(time.time()))
        logger.info("中文测试结束。。。")
    
    
    if __name__ == "__main__":
        my_logging.load_my_logging_cfg()  # 在你程序文件的入口加载自定义logging配置
        demo()

    3. Django log configuration file

    # logging_config.py
    # 学习中遇到问题没人解答?小编创建了一个Python学习交流群:711312441
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                          '[%(levelname)s][%(message)s]'
            },
            'simple': {
                'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
            },
            'collect': {
                'format': '%(message)s'
            }
        },
        'filters': {
            'require_debug_true': {
                '()': 'django.utils.log.RequireDebugTrue',
            },
        },
        'handlers': {
            # 打印到终端的日志
            'console': {
                'level': 'DEBUG',
                'filters': ['require_debug_true'],
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
            # 打印到文件的日志,收集info及以上的日志
            'default': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 5,  # 日志大小 5M
                'backupCount': 3,
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            # 打印到文件的日志:收集错误及以上的日志
            'error': {
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_err.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 5,  # 日志大小 5M
                'backupCount': 5,
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            # 打印到文件的日志
            'collect': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_collect.log"),
                'maxBytes': 1024 * 1024 * 5,  # 日志大小 5M
                'backupCount': 5,
                'formatter': 'collect',
                'encoding': "utf-8"
            }
        },
        'loggers': {
            # logging.getLogger(__name__)拿到的logger配置
            '': {
                'handlers': ['default', 'console', 'error'],
                'level': 'DEBUG',
                'propagate': True,
            },
            # logging.getLogger('collect')拿到的logger配置
            'collect': {
                'handlers': ['console', 'collect'],
                'level': 'INFO',
            }
        },
    }
    
    
    # -----------
    # 用法:拿到俩个logger
    
    logger = logging.getLogger(__name__)  # 线上正常的日志
    collect_logger = logging.getLogger("collect")  # 领导说,需要为领导们单独定制领导们看的日志

    The above is the detailed content of How to use the logging module in Python. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete