Home Backend Development Python Tutorial How to use the logging module in Python

How to use the logging module in Python

May 12, 2023 pm 06:10 PM
python logging

    ##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('严重错误信息')
    Copy after login

    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
    Copy after login

    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')
      Copy after login
    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()
    Copy after login

    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()
    Copy after login

    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")  # 领导说,需要为领导们单独定制领导们看的日志
    Copy after login

    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 of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

    VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

    Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

    VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

    How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

    In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

    Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

    VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

    Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

    VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

    Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

    The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

    See all articles