首页主机资讯Python在Linux中的日志如何配置

Python在Linux中的日志如何配置

时间2025-12-02 22:07:04发布访客分类主机资讯浏览1483
导读:在Linux中配置Python应用程序的日志,通常使用Python的内置logging模块。以下是一个基本的配置示例,展示了如何设置日志记录到文件和控制台: 基本配置: import logging # 配置日志记录到文件和控制台 l...

在Linux中配置Python应用程序的日志,通常使用Python的内置logging模块。以下是一个基本的配置示例,展示了如何设置日志记录到文件和控制台:

  1. 基本配置

    import logging
    
    # 配置日志记录到文件和控制台
    logging.basicConfig(
        level=logging.DEBUG,  # 设置日志级别
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',  # 设置日志格式
        handlers=[
            logging.FileHandler('app.log'),  # 日志文件
            logging.StreamHandler()  # 控制台输出
        ]
    )
    
    # 示例日志记录
    logging.debug('This is a debug message')
    logging.info('This is an info message')
    logging.warning('This is a warning message')
    logging.error('This is an error message')
    logging.critical('This is a critical message')
    
  2. 更高级的配置: 使用logging.config.dictConfiglogging.config.fileConfig可以进行更高级的配置。

    使用字典配置

    import logging
    import logging.config
    
    logging_config = {
    
        'version': 1,
        'formatters': {
    
            'standard': {
    
                'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            }
    ,
        }
    ,
        'handlers': {
    
            'default': {
    
                'level': 'DEBUG',
                'class': 'logging.FileHandler',
                'filename': 'app.log',
                'formatter': 'standard',
            }
    ,
            'console': {
    
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'standard',
            }
    ,
        }
    ,
        'loggers': {
    
            '': {
      # root logger
                'handlers': ['default', 'console'],
                'level': 'DEBUG',
                'propagate': True,
            }
    ,
        }
    
    }
        
    
    logging.config.dictConfig(logging_config)
    
    # 示例日志记录
    logging.debug('This is a debug message')
    logging.info('This is an info message')
    logging.warning('This is a warning message')
    logging.error('This is an error message')
    logging.critical('This is a critical message')
    

    使用文件配置: 创建一个logging.conf文件:

    [loggers]
    keys=root
    
    [handlers]
    keys=consoleHandler,fileHandler
    
    [formatters]
    keys=standardFormatter
    
    [logger_root]
    level=DEBUG
    handlers=consoleHandler,fileHandler
    
    [handler_consoleHandler]
    class=StreamHandler
    level=DEBUG
    formatter=standardFormatter
    args=(sys.stdout,)
    
    [handler_fileHandler]
    class=FileHandler
    level=DEBUG
    formatter=standardFormatter
    args=('app.log', 'a')
    
    [formatter_standardFormatter]
    format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    datefmt=%Y-%m-%d %H:%M:%S
    

    然后在Python代码中加载配置:

    import logging
    import logging.config
    
    logging.config.fileConfig('logging.conf')
    
    # 示例日志记录
    logging.debug('This is a debug message')
    logging.info('This is an info message')
    logging.warning('This is a warning message')
    logging.error('This is an error message')
    logging.critical('This is a critical message')
    

通过这些方法,你可以灵活地配置Python应用程序的日志记录,包括日志级别、格式、输出目标等。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Python在Linux中的日志如何配置
本文地址: https://pptw.com/jishu/761730.html
Debian消息系统中如何处理并发请求 Linux Python项目如何部署

游客 回复需填写必要信息