Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Rotating file handler not rotating

Tags:

python

logging

My rotatingfilehandler is set to 512 bytes with three backups. However the file does not rotate and is currently at 3.9MB What am I doing wrong? I am using Ubuntu 10.10, I also have the same code on a couple of Debian images which display the same issue so I assuming it is my code at fault.

[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'))
like image 424
Eric Hewett Avatar asked Oct 10 '12 09:10

Eric Hewett


2 Answers

I had same problem. Try with :

[handler_rfileHandler]
class=handlers.RotatingFileHandler
formatter=simpleFormatter
level=INFO
args=('/blabla/logs/blabla.log', 'a', 512, 3)
like image 91
may Avatar answered Oct 12 '22 05:10

may


I just had the same problem, with maxBytes and backupCount coming from a config file as well.

The following code didn't work:

handler  = logging.handlers.RotatingFileHandler(filename    = config.get("Logger", 'file'), 
                                                maxBytes    = config.get("Logger", 'max_file_size'), 
                                                backupCount = config.get("Logger", 'files_to_keep'))

It turned out that the problem was the way to get the parameters. The following code that passes integer values instead of strings works perfectly:

handler  = logging.handlers.RotatingFileHandler(filename    = config.get("Logger", 'file'), 
                                                maxBytes    = config.getint("Logger", 'max_file_size'), 
                                                backupCount = config.getint("Logger", 'files_to_keep'))
like image 34
nIcO Avatar answered Oct 12 '22 04:10

nIcO