I have the below config file which I am using to configure logging in my python modules , the issue with this is that it does not append the timestamp to the file-name , right now the log file created is file_handler.log but I want it to be file_handler-timestamp.log
{
"version": 1,
"disable_existing_loggers": true,
"formatters": {
"simple": {
"format": "%(asctime)s %(levelname)s %(filename)s %(lineno)s %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"file_handler": {
"level": "INFO",
"class": "logging.handlers.TimedRotatingFileHandler",
"formatter": "simple",
"filename": "error.log",
"backupCount": 10,
"interval" : 1,
"when": "s",
"encoding": "utf8"
}
},
"loggers": { },
"root": {
"handlers": [
"file_handler"
],
"level": "DEBUG"
}
}
Working code without config
import logging
import time
from logging.handlers import RotatingFileHandler
logger = logging.getLogger('log_1')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S')
handler = RotatingFileHandler('log' + time.strftime('_%Y%m%d-%H%M') + '.log', maxBytes=2000, backupCount=10)
handler.setFormatter(formatter)
logger.addHandler(handler)
Workaround code which updated the config dynamically when program is initiated
import logging.config
import json
import time
fp = open("sample_config.json")
config = json.load(fp)
fp.close()
config['handlers']['file_handler']['filename'] += time.strftime('_%Y%m%d-%H%M') + '.log'
logging.config.dictConfig(config)