我的rotatefilehandler设置为512字节,有三个备份。但是文件不旋转,目前为3.9MB我做错了什么?我正在使用Ubuntu 10.10,我在几个Debian图像上也有相同的代码,显示相同的问题所以我假设它是我的代码有错。
[formatters]
keys=simpleFormatter
[loggers]
keys=root,simpleExample
[handlers]
keys=consoleHandler,rfileHandler
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt= '%Y-%m-%d %H:%M:%S'
[logger_root]
level=INFO
handlers=consoleHandler,rfileHandler,email
[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)
[handler_rfileHandler]
class=handlers.RotatingFileHandler
backupCount=3
maxBytes=512
formatter=simpleFormatter
level=INFO
args=('/blabla/logs/blabla.log',)
[logger_simpleExample]
level=INFO
handlers=consoleHandler
qualname=simpleExample
propagate=0
[handler_email]
class=handlers.SMTPHandler
level=WARNING
formatter=simpleFormatter
args=('target url','sendemail',['erich@*****.com'],'EGIM Outstation Logger',('username','pw'))
答案 0 :(得分:4)
我有同样的问题。试试:
[handler_rfileHandler]
class=handlers.RotatingFileHandler
formatter=simpleFormatter
level=INFO
args=('/blabla/logs/blabla.log', 'a', 512, 3)
答案 1 :(得分:4)
我遇到了同样的问题,maxBytes
和backupCount
来自配置文件。
以下代码不起作用:
handler = logging.handlers.RotatingFileHandler(filename = config.get("Logger", 'file'),
maxBytes = config.get("Logger", 'max_file_size'),
backupCount = config.get("Logger", 'files_to_keep'))
事实证明问题是获取参数的方法。传递整数值而不是字符串的以下代码完美地运行:
handler = logging.handlers.RotatingFileHandler(filename = config.get("Logger", 'file'),
maxBytes = config.getint("Logger", 'max_file_size'),
backupCount = config.getint("Logger", 'files_to_keep'))