TimedRotatingFileHandler not working for minute intervals
I have configured a couple of TimedRotatingFileHandlers like this.
[loggers]
keys=root
[handlers]
keys=logfile, logfile_errors
[formatters]
keys=logfileformatter
[logger_root]
level=DEBUG
handlers=logfile, logfile_errors
[formatter_logfileformatter]
format=%(asctime)s %(name)-12s: %(levelname)s %(message)s
[handler_logfile]
class=handlers.TimedRotatingFileHandler
level=DEBUG
args=('./logs/log.out','S', 1, 10)
formatter=logfileformatter
[handler_logfile_errors]
class=handlers.TimedRotatingFileHandler
level=ERROR
args=('./logs/errors/log.out','M', 1, 10)
formatter=logfileformatter
However, when I log something like this in different minutes
logging.error('MY ERROR')
Backup files (1 per second) are created for the logfile handler but not for the logfile_errors handler (I would expect one backup file per minute in which an error was logged). Am I doing something wrong when configuring the handlers?
Solution 1:
I don't see any error in your config... it works on my machine :-)
Here is a minimal working example so you can check on your side, using your config without any modification:
import logging
import logging.config
import time
logging.config.fileConfig('mytestlog.conf')
while True:
logging.error('other error message')
time.sleep(0.5);
Resulting files after running about 3 minutes:
$ ls logs | sort
errors
log.out
log.out.2022-02-09_09-54-20
log.out.2022-02-09_09-54-21
log.out.2022-02-09_09-54-22
log.out.2022-02-09_09-54-23
log.out.2022-02-09_09-54-24
log.out.2022-02-09_09-54-25
log.out.2022-02-09_09-54-26
log.out.2022-02-09_09-54-27
log.out.2022-02-09_09-54-28
log.out.2022-02-09_09-54-29
$ ls logs/errors/ | sort
log.out
log.out.2022-02-09_09-51
log.out.2022-02-09_09-52
Sample contents:
$ cat logs/log.out.2022-02-09_09-54-20
2022-02-09 09:54:20,109 root : ERROR other error message
2022-02-09 09:54:20,610 root : ERROR other error message
$ cat logs/errors/log.out.2022-02-09_09-52
2022-02-09 09:52:54,367 root : ERROR other error message
2022-02-09 09:52:54,869 root : ERROR other error message
2022-02-09 09:52:55,370 root : ERROR other error message
2022-02-09 09:52:55,872 root : ERROR other error message
2022-02-09 09:52:56,373 root : ERROR other error message
2022-02-09 09:52:56,875 root : ERROR other error message
2022-02-09 09:52:57,376 root : ERROR other error message
..
..
2022-02-09 09:53:52,536 root : ERROR other error message
2022-02-09 09:53:53,037 root : ERROR other error message
2022-02-09 09:53:53,540 root : ERROR other error message