EMAI
EMAI

Reputation: 744

logging TimedRotatingFileHandler don't work correctly

TimedRotatingFileHandler in logging package not work correctly. My program source:

from logging.handlers import TimedRotatingFileHandler
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = TimedRotatingFileHandler("log/log_file.log", when="d")
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

After a few days, two problems arise. First, it writes in log_file.log again. Second, logging logs the events of day A on day B. For example:

File name:    
log_file.log.2018-06-21

and

File content:

2018-06-23 08:05:42,906 - uwsgi_file__home_ddddd_main - INFO - 
2018-06-23 08:05:42,907 - uwsgi_file__home_ddddd_main - DEBUG -

In this example, by mistake, the events of June 23 was written in the file of June 21st.

Upvotes: 2

Views: 1807

Answers (1)

Vinay Sajip
Vinay Sajip

Reputation: 99317

This handler always writes to the base file (xxx.log). When the time comes to rollover, then the file is closed, renamed and a file with the base filename is opened again.

If you are using multiple process to write to the same file (implied by your possible use of UWSGI) then you might have occasional problems - writing to the same file from multiple processes is supported, but not the way you've done it. See this question/answer and this cookbook entry for more information.

Upvotes: 2

Related Questions