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
The idea is simple:
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')
yaml_config
is where the action is. I first load the configuration dictionary from the YAML file, then patch up the file name dictConfig
to finish the job
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.