python日志模块 - 终端,stdout,stderr

时间:2017-12-13 14:25:24

标签: python-2.7 logging

我想使用日志记录模块将我的输出从python(2.7)脚本记录到三个流:

  1. 到终端/标准输出(根记录器)
  2. 到包含所有消息的日志文件(完整记录器,类似于stdout)
  3. 到仅包含警告和错误消息的日志文件(错误记录器,类似于stderr)
  4. 记录器2.和3.需要是RotatingFileHandlers(maxBytes:1024,backupCount:3),在一段时间后覆盖旧的历史日志文件。

    通过以下设置,它成功地将日志以所需格式写入终端,并创建stdout.log和stderr.log文件,但两个文件都显示为空。

    有关正确填充日志文件的缺失的任何提示吗?

    这是测试脚本

    test_logger.py

    import logging.config
    
    logging.config.fileConfig('logging.conf') 
    
    logging.debug("DEBUG MESSAGE")
    logging.info("INFO MESSAGE")
    logging.warning("WARNING MESSAGE")
    logging.error("ERROR MESSAGE")
    
    a = []
    try :
        b = a[0]
    except :
        logging.exception("EXCEPTION MESSAGE")
    # end try
    

    这是记录器的配置

    logging.conf

    [loggers]
    keys=root,fullLogger,errorLogger
    
    [handlers]
    keys=rootHandler,fullHandler,errorHandler
    
    [formatters]
    keys=simpleFormatter
    
    [logger_root]
    level=DEBUG
    handlers=rootHandler
    propagate=1
    
    [logger_fullLogger]
    level=DEBUG
    handlers=fullHandler
    qualname=fullLogger
    propagate=1
    
    [logger_errorLogger]
    level=WARNING
    handlers=errorHandler
    qualname=errorLogger
    propagate=1
    
    [handler_rootHandler]
    class=StreamHandler
    level=DEBUG
    formatter=simpleFormatter
    args=(sys.stdout,)
    
    [handler_fullHandler]
    class=logging.handlers.RotatingFileHandler
    level=DEBUG
    formatter=simpleFormatter
    args=('stdout.log','w',1024,3)
    
    [handler_errorHandler]
    class=logging.handlers.RotatingFileHandler
    level=WARNING
    formatter=simpleFormatter
    args=('stderr.log','w',1024,3)
    
    [formatter_simpleFormatter]
    format=[%(asctime)s.%(msecs)03d] %(levelname)s :: %(message)s
    datefmt=%Y-%m-%d %H:%M:%S
    

    我期待以下输出:

    终端

    [2017-12-13 15:18:59.265] DEBUG :: DEBUG MESSAGE
    [2017-12-13 15:18:59.265] INFO :: INFO MESSAGE
    [2017-12-13 15:18:59.265] WARNING :: WARNING MESSAGE
    [2017-12-13 15:18:59.265] ERROR :: ERROR MESSAGE
    [2017-12-13 15:18:59.265] ERROR :: EXCEPTION MESSAGE
    Traceback (most recent call last):
      File "C:\path\test_logger2.py", line 12, in <module>
        b = a[0]
    IndexError: list index out of range
    

    stdout.log

    [2017-12-13 15:18:59.265] DEBUG :: DEBUG MESSAGE
    [2017-12-13 15:18:59.265] INFO :: INFO MESSAGE
    [2017-12-13 15:18:59.265] WARNING :: WARNING MESSAGE
    [2017-12-13 15:18:59.265] ERROR :: ERROR MESSAGE
    [2017-12-13 15:18:59.265] ERROR :: EXCEPTION MESSAGE
    Traceback (most recent call last):
      File "C:\path\test_logger2.py", line 12, in <module>
        b = a[0]
    IndexError: list index out of range
    

    stderr.log

    [2017-12-13 15:18:59.265] WARNING :: WARNING MESSAGE
    [2017-12-13 15:18:59.265] ERROR :: ERROR MESSAGE
    [2017-12-13 15:18:59.265] ERROR :: EXCEPTION MESSAGE
    Traceback (most recent call last):
      File "C:\path\test_logger2.py", line 12, in <module>
        b = a[0]
    IndexError: list index out of range
    

1 个答案:

答案 0 :(得分:1)

必须将三个处理程序组合在一个根记录器下:

CType(Application.Current.Resources("button_bg"), Style).Setters.Add(New Setter(System.Windows.Shapes.Path.DataProperty, Application.Current.Resources("circleBackground")))