I\'m using the logging module in Python and I would like it to create a new logfile each time my application is started. The older logfiles shoud be rotated (eg: logfile.txt
I had a similar requirement to be able to force a log rotation at startup based on a command-line option, but for the logfiles to otherwise rotate on their regular schedule. This was my solution:
import logging
from logging.handlers import BaseRotatingHandler
from typing import Union
def rotate_logs(loggers: Union[str,list]=None, delimiter: str=','):
"""Rotate logs.
Args:
loggers: List of logger names as list object or as string,
separated by `delimiter`.
delimiter: Separator for logger names, if `loggers` is :obj:`str`.
Defaults to ``,`` (comma).
"""
# Convert loggers to list.
if isinstance(loggers, str):
loggers = [t.strip() for t in loggers.split(delimiter)]
handlers = []
root = logging.getLogger()
# Include root logger in dict.
ld = {'': root, **root.manager.loggerDict}
for k, v in ld.items():
if loggers is not None and k not in loggers:
continue
try:
for h in v.handlers:
if (isinstance(h, BaseRotatingHandler) and
h not in handlers):
handlers.append(h)
except AttributeError:
pass
for h in handlers:
h.doRollover()
if __name__ == '__main__':
pass
Notes:
This has been validated to work if maxBytes > 0 on a RotatingFileHandler.
This method hasn't been tested with a TimedRotatingFileHandler, but should work.
This method eliminates the need to maintain a reference to the RotatingFileHandler to be rotated; as a result, it can easily be used when configuring logging using logging.config.