From 5b6ec72d0b751c6823a9553c16cc2aed92784e84 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Mon, 23 Sep 2019 12:23:41 -0700 Subject: [PATCH] break out logging into separate module --- pinhook/bot.py | 38 +++++++++++++++++--------------------- pinhook/log.py | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 pinhook/log.py diff --git a/pinhook/bot.py b/pinhook/bot.py index b18c73a..c4d6cae 100644 --- a/pinhook/bot.py +++ b/pinhook/bot.py @@ -5,6 +5,8 @@ import logging import os import ssl import time + +from . import log from . import plugin import irc.bot @@ -22,6 +24,7 @@ class Bot(irc.bot.SingleServerIRCBot): self.ns_pass = kwargs.get('ns_pass', None) self.nickserv = kwargs.get('nickserv', 'NickServ') self.log_level = kwargs.get('log_level', 'info') + self.log_file = kwargs.get('log_file', None) self.server_pass = kwargs.get('server_pass', None) self.cmd_prefix = kwargs.get('cmd_prefix', '!') self.use_prefix_for_plugins = kwargs.get('use_prefix_for_plugins', False) @@ -32,7 +35,7 @@ class Bot(irc.bot.SingleServerIRCBot): irc.bot.SingleServerIRCBot.__init__(self, [(server, self.port, self.server_pass)], nickname, nickname) self.chanlist = channels self.bot_nick = nickname - self.start_logging(self.log_level) + self.start_logging() self.output_message = plugin.message self.output_action = plugin.action self.internal_commands = { @@ -67,31 +70,23 @@ class Bot(irc.bot.SingleServerIRCBot): if not (cmd or text): raise TypeError('missing cmd or text parameter') - def start_logging(self, level): - if level == 'error': + def start_logging(self): + self.logger = log.logger + if self.log_file: + log.set_log_file(self.log_file) + else: + log.set_log_file('{}.log'.format(self.bot_nick)) + if self.log_level == 'error': level = logging.ERROR - elif level == 'warning': + elif self.log_level == 'warning': level = logging.WARNING - elif level == 'info': + elif self.log_level == 'info': level = logging.INFO - elif level == 'debug': + elif self.log_level == 'debug': level = logging.DEBUG - self.logger = logging.getLogger(self.bot_nick) - formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s') - # Set console logger - ch = logging.StreamHandler() - ch.setFormatter(formatter) - # Set file logger - fh = logging.FileHandler('{}.log'.format(self.bot_nick)) - fh.setFormatter(formatter) # Set levels - if level != "off": + if self.log_level != "off": self.logger.setLevel(level) - ch.setLevel(level) - fh.setLevel(level) - # Add handlers - self.logger.addHandler(ch) - self.logger.addHandler(fh) self.logger.info('Logging started!') def load_plugins(self): @@ -304,7 +299,8 @@ class Bot(irc.bot.SingleServerIRCBot): class TwitchBot(Bot): def __init__(self, nickname, channel, token, plugin_dir='plugins', log_level='info', ops=[]): self.bot_nick = nickname - self.start_logging(log_level) + self.log_level = log_level + self.start_logging() self.channel = channel self.plugin_dir = plugin_dir self.ops = ops diff --git a/pinhook/log.py b/pinhook/log.py new file mode 100644 index 0000000..e6978e6 --- /dev/null +++ b/pinhook/log.py @@ -0,0 +1,14 @@ +import logging + +logger = logging.getLogger('bot') +formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s') +# Set console logger +streamhandler = logging.StreamHandler() +streamhandler.setFormatter(formatter) +logger.addHandler(streamhandler) + +def set_log_file(filename): + # Set file logger + filehandler = logging.FileHandler(filename) + filehandler.setFormatter(formatter) + logger.addHandler(filehandler) \ No newline at end of file