2015-12-07 02:40:13 +01:00
|
|
|
"""
|
|
|
|
log.py - PyLink logging module.
|
|
|
|
|
|
|
|
This module contains the logging portion of the PyLink framework. Plugins can
|
|
|
|
access the global logger object by importing "log" from this module
|
|
|
|
(from log import log).
|
|
|
|
"""
|
|
|
|
|
2015-07-05 22:22:17 +02:00
|
|
|
import logging
|
2015-07-07 21:14:55 +02:00
|
|
|
import sys
|
2015-07-19 05:11:29 +02:00
|
|
|
import os
|
|
|
|
|
2015-08-04 04:27:19 +02:00
|
|
|
from conf import conf, confname
|
2015-07-07 21:14:55 +02:00
|
|
|
|
2015-07-19 05:11:29 +02:00
|
|
|
level = conf['bot'].get('loglevel') or 'DEBUG'
|
2015-07-07 21:14:55 +02:00
|
|
|
try:
|
2015-07-19 05:11:29 +02:00
|
|
|
level = getattr(logging, level.upper())
|
2015-07-07 21:14:55 +02:00
|
|
|
except AttributeError:
|
|
|
|
print('ERROR: Invalid log level %r specified in config.' % level)
|
|
|
|
sys.exit(3)
|
|
|
|
|
2015-07-19 05:11:29 +02:00
|
|
|
curdir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
logdir = os.path.join(curdir, 'log')
|
|
|
|
# Make sure our log/ directory exists
|
|
|
|
os.makedirs(logdir, exist_ok=True)
|
|
|
|
|
|
|
|
_format = '%(asctime)s [%(levelname)s] %(message)s'
|
|
|
|
logging.basicConfig(level=level, format=_format)
|
|
|
|
|
|
|
|
# Set log file to $CURDIR/log/pylink
|
|
|
|
logformat = logging.Formatter(_format)
|
2015-08-04 04:27:19 +02:00
|
|
|
logfile = logging.FileHandler(os.path.join(logdir, '%s.log' % confname), mode='w')
|
2015-07-19 05:11:29 +02:00
|
|
|
logfile.setFormatter(logformat)
|
2015-07-05 22:22:17 +02:00
|
|
|
|
|
|
|
global log
|
|
|
|
log = logging.getLogger()
|
2015-07-19 05:11:29 +02:00
|
|
|
log.addHandler(logfile)
|
2016-01-23 22:13:38 +01:00
|
|
|
|
|
|
|
class PyLinkChannelLogger(logging.Handler):
|
|
|
|
"""
|
|
|
|
Log handler to log to channels in PyLink.
|
|
|
|
"""
|
|
|
|
def __init__(self, irc, channels):
|
|
|
|
super(PyLinkChannelLogger, self).__init__()
|
|
|
|
self.irc = irc
|
|
|
|
self.channels = channels
|
|
|
|
|
|
|
|
# Use a slightly simpler message formatter - logging to IRC doesn't need
|
|
|
|
# logging the time.
|
|
|
|
formatter = logging.Formatter('[%(levelname)s] %(message)s')
|
|
|
|
self.setFormatter(formatter)
|
|
|
|
|
|
|
|
# Log level has to be at least 20 (INFO) to prevent loops due
|
|
|
|
# to outgoing messages being logged
|
|
|
|
loglevel = max(log.getEffectiveLevel(), 20)
|
|
|
|
self.setLevel(loglevel)
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
"""
|
|
|
|
Logs a record to the configured channels for the network given.
|
|
|
|
"""
|
|
|
|
# Only start logging if we're finished bursting
|
|
|
|
if hasattr(self.irc, 'pseudoclient') and self.irc.connected.is_set():
|
|
|
|
msg = self.format(record)
|
|
|
|
for channel in self.channels:
|
|
|
|
self.irc.msg(channel, msg)
|
|
|
|
|