I'm using Python's logging.config
module to configure and use a logging tool in my project.
I want my log files to overwrite each time (not append), so I set my YAML configuration file like this:
# logging configuration fileversion: 1
disable_existing_loggers: Falseformatters:simple:format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"handlers:console:class: logging.StreamHandlerlevel: DEBUGformatter: simplestream: ext://sys.stdoutinfo_file_handler:class: logging.handlers.RotatingFileHandlerlevel: INFOformatter: simplefilename: .logs/info.logmaxBytes: 10485760 # 10MBbackupCount: 20encoding: utf8mode: 'w'error_file_handler:class: logging.handlers.RotatingFileHandlerlevel: ERRORformatter: simplefilename: .logs/errors.logmaxBytes: 10485760 # 10MBbackupCount: 20encoding: utf8mode: 'w'loggers:my_module:level: ERRORhandlers: [console]propagate: noroot:level: INFOhandlers: [console, info_file_handler, error_file_handler]
This question mentions that using mode: w
in the handler configuration should accomplish what I want, but the log files keep appending anyway. Here is my configuration code as well:
def logging_setup(cfg_path=definitions.LOG_CONFIG_PATH, def_lvl=logging.INFO):"""Setup logging tool from YAML configuration file."""# create directory for log files if not already theretry:os.makedirs(definitions.LOGS_PATH)except OSError as e:if e.errno != errno.EEXIST:raise# configure logging from yaml config fileif os.path.exists(cfg_path):with open(cfg_path, 'rt') as f:config = yaml.load(f.read())logging.config.dictConfig(config)else:logging.basicConfig(level=def_lvl)
What am I doing incorrectly?