Python 如何使用 logging.conf 文件使用 RotatingFileHandler 将所有内容记录到文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20154490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to log everything into a file using RotatingFileHandler by using logging.conf file?
提问by
I am trying to use RotatingHandlerfor our logging purpose in Python. I have kept backup files as 500 which means it will create maximum of 500 files I guess and the size that I have set is 2000 Bytes (not sure what is the recommended size limit is).
我正在尝试RotatingHandler在 Python 中用于我们的日志记录目的。我将备份文件保留为 500,这意味着我猜最多可以创建 500 个文件,我设置的大小为 2000 字节(不确定推荐的大小限制是多少)。
If I run my below code, it doesn't log everything into a file. I want to log everything into a file -
如果我运行下面的代码,它不会将所有内容都记录到文件中。我想将所有内容都记录到一个文件中 -
#!/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)
This is what gets printed out in my testing.logfile -
这是在我的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
It doesn't print out INFO, DEBUGmessage into the file somehow.. Any thoughts why it is not working out?
它不打印出来INFO,DEBUG以某种方式将消息发送到文件中.. 任何想法为什么它不起作用?
And also, right now, I have defined everything in this python file for logging purpose. I want to define above things in the logging conffile and read it using the fileConfig()function. I am not sure how to use the RotatingFileHandlerexample in the logging.conffile?
而且,现在,我已经定义了这个 python 文件中的所有内容,用于记录目的。我想在logging conf文件中定义上述内容并使用该fileConfig()函数读取它。我不确定如何使用文件中的RotatingFileHandler示例logging.conf?
UPDATE:-
更新:-
Below is my updated Python code that I have modified to use with log.conffile -
以下是我已修改以与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)
And below is my log.conf file-
下面是我的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
But whenever I compile it, this is the error I got on my console -
但是每当我编译它时,这就是我在控制台上遇到的错误 -
$ 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'
Any idea what wrong I am doing here?
知道我在这里做错了什么吗?
采纳答案by Gryphius
It doesn't print out INFO, DEBUG message into the file somehow.. Any thoughts why it is not working out?
它不会以某种方式将 INFO、DEBUG 消息打印到文件中。有什么想法为什么它不起作用?
you don't seem to set a loglevel, so the default (warning) is used
您似乎没有设置日志级别,因此使用默认值(警告)
from http://docs.python.org/2/library/logging.html:
来自http://docs.python.org/2/library/logging.html:
Note that the root logger is created with level WARNING.
请注意,根记录器是使用级别 WARNING 创建的。
as for your second question, something like this should do the trick (I haven't tested it, just adapted from my config which is using the TimedRotatingFileHandler):
至于你的第二个问题,这样的事情应该可以解决问题(我还没有测试过,只是根据我使用 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
回答by Sanjay
I know, it is very late ,but I just got same error, and while searching that are I got your problem. I am able to resolve my problem, and I thought it might be helpful for some other user also :
我知道,现在已经很晚了,但我遇到了同样的错误,在搜索时我遇到了您的问题。我能够解决我的问题,我认为它可能对其他一些用户也有帮助:
you have created a logger object and trying to access my_logger.config.fileConfig('log.conf')which is wrong you should use logger.config.fileConfig('log.conf')as I mention below and need to import logging.configas well :
您已经创建了一个记录器对象并尝试访问my_logger.config.fileConfig('log.conf')这是错误的,您应该使用logger.config.fileConfig('log.conf') ,正如我在下面提到的,并且需要导入日志记录。配置以及:
#!/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')
after doing these changes, AttributeError: 'Logger' object has no attribute 'config'error must be gone.
完成这些更改后,AttributeError: 'Logger' object has no attribute 'config'错误必须消失。

