From 59880f6bf18c94c6e1974c8b3d30fc54b5af06a1 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 16 Oct 2016 17:01:36 -0400 Subject: [PATCH] Rotate the log --- rtmbot/core.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/rtmbot/core.py b/rtmbot/core.py index 3d91ab4..75939b8 100755 --- a/rtmbot/core.py +++ b/rtmbot/core.py @@ -5,11 +5,15 @@ import glob import os import time import logging +import logging.handlers from slackclient import SlackClient sys.dont_write_bytecode = True +LOGFILE_MAX_BYTES = 50 * 1024 * 1024 # 50 MB +LOGFILE_BACKUPS = 5 + class RtmBot(object): def __init__(self, config): @@ -40,10 +44,14 @@ class RtmBot(object): self.debug = self.config.get('DEBUG', False) # establish logging + root_logger = logging.getLogger() log_file = config.get('LOGFILE', 'rtmbot.log') - logging.basicConfig(filename=log_file, - level=logging.DEBUG if self.debug else logging.INFO, - format='%(asctime)s %(module)s %(levelname)s: %(message)s') + root_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=LOGFILE_MAX_BYTES, backupCount=LOGFILE_BACKUPS) + root_formatter = logging.Formatter('%(asctime)s %(module)s %(levelname)s: %(message)s') + root_handler.setFormatter(root_formatter) + root_logger.addHandler(root_handler) + root_logger.setLevel(logging.DEBUG if self.debug else logging.INFO) + logging.info('Initialized in: {}'.format(self.directory)) # initialize stateful fields