1

I'm using logger module with dictConfig (with a yaml file) for my application logging. However, I would to change the log file name globally by adding a date prefix (ex: logpath: /foo/bar/file_20150523.log).

So each time I start my application a new log file is created.

Is it possible in one way of another to do it inside the yaml file or I need to modify the handler in my application ?

Thanx

1
  • After you load the yaml file, append the current date to the file name, then call dictConfig. I have never used dictConfig before, so I need some time to learn before I can provide detailed answer. Commented May 23, 2015 at 22:51

1 Answer 1

6

The idea is simple:

  1. Read configuration from a YAML file
  2. Locate the name of the log file
  3. Append the date stamp to the name part (not the extension part)
  4. 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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.