【问题标题】:How to log everything into a file using RotatingFileHandler by using logging.conf file?如何使用 logging.conf 文件使用 RotatingFileHandler 将所有内容记录到文件中?
【发布时间】:2013-12-07 21:38:27
【问题描述】:

我正在尝试使用RotatingHandler 在 Python 中进行日志记录。我将备份文件保留为 500,这意味着我猜它最多会创建 500 个文件,我设置的大小是 2000 字节(不确定推荐的大小限制是多少)。

如果我运行下面的代码,它不会将所有内容都记录到文件中。我想将所有内容都记录到一个文件中 -

#!/usr/bin/python

import logging
import logging.handlers

LOG_FILENAME = 'testing.log'

# Set up a specific logger with our desired output level
my_logger = logging.getLogger('agentlogger')

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=2000, backupCount=100)

# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

my_logger.addHandler(handler)

my_logger.debug('debug message')
my_logger.info('info message')
my_logger.warn('warn message')
my_logger.error('error message')
my_logger.critical('critical message')

# Log some messages
for i in range(10):
    my_logger.error('i = %d' % i)

这是在我的testing.log 文件中打印出来的内容 -

2013-11-22 12:59:34,782 - agentlogger - WARNING - warn message
2013-11-22 12:59:34,782 - agentlogger - ERROR - error message
2013-11-22 12:59:34,782 - agentlogger - CRITICAL - critical message
2013-11-22 12:59:34,782 - agentlogger - ERROR - i = 0
2013-11-22 12:59:34,782 - agentlogger - ERROR - i = 1
2013-11-22 12:59:34,783 - agentlogger - ERROR - i = 2
2013-11-22 12:59:34,783 - agentlogger - ERROR - i = 3
2013-11-22 12:59:34,783 - agentlogger - ERROR - i = 4
2013-11-22 12:59:34,783 - agentlogger - ERROR - i = 5
2013-11-22 12:59:34,783 - agentlogger - ERROR - i = 6
2013-11-22 12:59:34,784 - agentlogger - ERROR - i = 7
2013-11-22 12:59:34,784 - agentlogger - ERROR - i = 8
2013-11-22 12:59:34,784 - agentlogger - ERROR - i = 9

它不会以某种方式将INFODEBUG 消息打印到文件中。任何想法为什么它不起作用?

而且,现在,我已经在这个 python 文件中定义了所有内容以用于日志记录。我想在logging conf 文件中定义上述内容并使用fileConfig() 函数读取它。我不确定如何使用logging.conf 文件中的RotatingFileHandler 示例?

更新:-

以下是我修改后用于log.conf 文件的更新 Python 代码 -

#!/usr/bin/python

import logging
import logging.handlers

my_logger = logging.getLogger(' ')
my_logger.config.fileConfig('log.conf')

my_logger.debug('debug message')
my_logger.info('info message')
my_logger.warn('warn message')
my_logger.error('error message')
my_logger.critical('critical message')

# Log some messages
for i in range(10):
    my_logger.error('i = %d' % i)

下面是我的log.conf file -

[loggers]
keys=root

[handlers]
keys=logfile

[formatters]
keys=logfileformatter

[logger_root]
level=DEBUG
handlers=logfile

[logger_zkagentlogger]
level=DEBUG
handlers=logfile
qualname=zkagentlogger
propagate=0

[formatter_logfileformatter]
format=%(asctime)s %(name)-12s: %(levelname)s %(message)s

[handler_logfile]
class=handlers.RotatingFileHandler
level=NOTSET
args=('testing.log',2000,100)
formatter=logfileformatter

但是每当我编译它时,这就是我在控制台上遇到的错误 -

$ python logtest3.py
Traceback (most recent call last):
  File "logtest3.py", line 6, in <module>
    my_logger.config.fileConfig('log.conf')
AttributeError: 'Logger' object has no attribute 'config'

知道我在这里做错了什么吗?

【问题讨论】:

    标签: python logging file-io


    【解决方案1】:

    它不会以某种方式将 INFO、DEBUG 消息打印到文件中。任何 想为什么它不起作用?

    您似乎没有设置日志级别,所以使用默认(警告)

    来自http://docs.python.org/2/library/logging.html

    请注意,根记录器是使用 WARNING 级别创建的。

    至于你的第二个问题,这样的事情应该可以解决问题(我没有测试过,只是改编自我使用 TimedRotatingFileHandler 的配置):

    [loggers]
    keys=root
    
    [handlers]
    keys=logfile
    
    [formatters]
    keys=logfileformatter
    
    [logger_root]
    level=DEBUG
    handlers=logfile
    
    [formatter_logfileformatter]
    format=%(asctime)s %(name)-12s: %(levelname)s %(message)s
    
    [handler_logfile]
    class=handlers.RotatingFileHandler
    level=NOTSET
    args=('testing.log','a',2000,100)
    formatter=logfileformatter
    

    【讨论】:

    • 谢谢.. 那么我应该设置什么来将所有内容记录到文件中?
    • 我添加了一些我在项目中使用的工作配置的摘录,这应该可以帮助您入门。可以找到更多记录器的完整示例github.com/gryphius/fuglu/blob/master/fuglu/conf/…
    • 感谢 Gryphius 的帮助。最后一个疑问,那么我将如何在 python 代码中获取记录器?有什么想法吗?这是我第一次使用 Python,所以遇到了一些问题。
    • 您获取记录器的方式与您在代码中所做的相同 (logging.getLogger)。见this example
    • 我认为args行有一个小错误:需要包含文件模式:args=('testing.log','a',2000,100)
    【解决方案2】:

    我知道,已经很晚了,但我也遇到了同样的错误,在搜索时我遇到了你的问题。我能够解决我的问题,并且我认为它可能对其他一些用户也有帮助:

    您创建了一个记录器对象并尝试访问 my_logger.config.fileConfig('log.conf') 这是错误的,您应该使用 logger.config.fileConfig('log. conf') 正如我在下面提到的,并且还需要导入 logging.config

    #!/usr/bin/python
    
    import logging
    import logging.handlers
    import logging.config
    
    logging.config.fileConfig('log.config',disable_existing_loggers=0)
    my_logger = logging.getLogger('you logger name as you mention in your conf file')
    
    my_logger.debug('debug message')
    my_logger.info('info message')
    my_logger.warn('warn message')
    my_logger.error('error message')
    my_logger.critical('critical message')
    

    进行这些更改后,AttributeError: 'Logger' object has no attribute 'config' 错误必须消失。

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多