I wrote a code with a function to create a logger that sends logs to the console and to a file. To save space, I want to archive old log files and add the date of archiving to their name.
import logging
import os
import datetime
import logging.handlers
log_dir = 'logs'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
def get_logger(name):
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
max_bytes = 100 * 1024 * 1024 # 100 Mb
file_handler = logging.handlers.RotatingFileHandler(
filename= f'{log_dir}/{name}.log',
maxBytes=max_bytes,
encoding='utf-8'
)
file_handler.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.ERROR)
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(f_format)
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(c_format)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
I suppose simply renaming the file extension will not give any results. I did not find any hints for archiving in the documentation. There is a topic here with a similar problem, where a new class is created based on the RotatingFileHandler, but this solution is from 2011, and in my case, apparently, it doesn’t work very well, or I somehow didn’t apply it correctly.
Perhaps you missed this bit? The following example illustrates:
After running this, you will see six new files, five of which are compressed:
You can adapt this to a different compression scheme, if required.