The idea is simple:
- Read configuration from a YAML file
- Locate the name of the log file
- Append the date stamp to the name part (not the extension part)
- Call
logging.config.dictConfig and pass in the modified configuration dictionary
Here is the YAML file I use for this example, daily_log_file.yaml:
version: 1 loggers: default_logger: handlers: [consoleHandler, fileHandler] level: DEBUG handlers: consoleHandler: class: logging.StreamHandler level: DEBUG formatter: brief fileHandler: class: logging.FileHandler formatter: brief filename: '/tmp/daily_log_file.log' level: DEBUG formatters: brief: format: '%(levelname)8s %(message)s'
Here is the script, daily_log_file.py:
import datetime import os import logging import logging.config import yaml def yaml_config(yaml_filename): global config_dict with open(yaml_filename) as f: config_dict = yaml.load(f) # Append the date stamp to the file name log_filename = config_dict['handlers']['fileHandler']['filename'] base, extension = os.path.splitext(log_filename) today = datetime.datetime.today() log_filename = '{}{}{}'.format( base, today.strftime('_%Y%m%d'), extension) config_dict['handlers']['fileHandler']['filename'] = log_filename # Apply the configuration logging.config.dictConfig(config_dict) if __name__ == '__main__': yaml_config('daily_log_file.yaml') logger = logging.getLogger('default_logger') logger.debug('debug message') logger.info('info message')
Discussion
yaml_config is where the action is. I first load the configuration dictionary from the YAML file, then patch up the file name - To patch up the file name, I separate the base file name and extension
- I then get today's date and append the time stamp to the file name, then assign the result back to the configuration dictionary
- Finally, I called
dictConfig to finish the job
dictConfig. I have never useddictConfigbefore, so I need some time to learn before I can provide detailed answer.