Skip to content

Commit

Permalink
Add log_backup_count and log_max_bytes settings.
Browse files Browse the repository at this point in the history
This adds config options to control the log file rotation.
  • Loading branch information
bwduncan committed Jan 3, 2020
1 parent 45bfe44 commit a360874
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/emonhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ def _update_settings(self, settings):
else:
self._set_logging_level()

if 'log_backup_count' in settings['hub']:
for handler in self._log.handlers:
if isinstance(handler, logging.handlers.RotatingFileHandler):
handler.backupCount = settings['hub']['log_backup_count']
self._log.info("Logging backup count set to %d", handler.backupCount)
if 'log_max_bytes' in settings['hub']:
for handler in self._log.handlers:
if isinstance(handler, logging.handlers.RotatingFileHandler):
handler.maxBytes = settings['hub']['log_max_bytes']
self._log.info("Logging max file size set to %d bytes", handler.maxBytes)

# Interfacers
for name in self._interfacers:
Expand Down Expand Up @@ -279,8 +289,10 @@ def _set_logging_level(self, level='WARNING', log=True):
# this ensures it is writable
# Close the file for now and get its path
args.logfile.close()
# These maxBytes and backupCount defaults can be overridden in the config file.
loghandler = logging.handlers.RotatingFileHandler(args.logfile.name,
'a', 5000 * 1024, 1)
maxBytes=5000 * 1024,
backupCount=1)
else:
# Otherwise, if no path was specified, everything goes to sys.stderr
loghandler = logging.StreamHandler()
Expand Down

0 comments on commit a360874

Please sign in to comment.