Python Logging - Configuring Rotating File Handler From Config file

2.5k Views Asked by At

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)
1

There are 1 best solutions below

6
On BEST ANSWER

timestamp of when? When the file is rotated or when it is created?

If you use TimedRotatingFileHandler, then when the file is rotated, the timestamp is appended to the filename.

If you want the timestamp appended to the log filename, you have to subclass RotatingFileHandler and modify the __init__ method. Then point the "class" item of "file_handler" of the config file to your new class.

It is easy to do, but it may not be what you need. Just let the timestamp be appended to the rotated log filename or if you need to limit the size too, read this answer