Is it possible to create log files with custom date format, using TimedRotatingFileHandler
?
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime
import time
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
log_dir = 'logs'
log_file = os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + f'_.log')
handler = TimedRotatingFileHandler(log_file, when='s', interval=1, backupCount=0)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
i = 0
while True:
logger.info(i)
print(i)
time.sleep(1)
i += 1
Produces
20230810-100547_.log.2023-08-10_10-05-47
20230810-100547_.log.2023-08-10_10-05-48
20230810-100547_.log
Instead of
20230810-100547_.log
20230810-100548_.log
20230810-100549_.log
Does one need to reset the handler after midnight (is it even possible)? Does not seem elegant.
There seem to be a namer attribute in logging handlers
There is rotation_filename
method and you can assign it to namer function. It will change name of your log file during rotation. Working code should look like this:
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime
import time
def namer(self):
now = datetime.now()
log_dir = './logs'
return os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + f'_.log')
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
log_dir = './logs'
log_file = os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + f'_.log')
handler = TimedRotatingFileHandler(log_file, when='s', interval=1, backupCount=0)
handler.rotation_filename = namer
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
i = 0
while True:
logger.info(i)
print(i)
time.sleep(1)
i += 1