3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

core: support multiple channel loggers with DIFFERENT log levels & fix example conf (#83)

This commit is contained in:
James Lu 2016-01-30 23:33:03 -08:00
parent 669e889e6f
commit 0d4655c381
4 changed files with 39 additions and 25 deletions

View File

@ -37,7 +37,7 @@ class Irc():
(a string), the name of the protocol module to use for this connection, (a string), the name of the protocol module to use for this connection,
and a configuration object. and a configuration object.
""" """
self.loghandler = None self.loghandlers = []
self.name = netname.lower() self.name = netname.lower()
self.conf = conf self.conf = conf
self.serverdata = conf['servers'][netname] self.serverdata = conf['servers'][netname]
@ -72,11 +72,19 @@ class Irc():
log.debug('(%s) Setting up channel logging to channels %r', self.name, log.debug('(%s) Setting up channel logging to channels %r', self.name,
channels) channels)
if channels and not self.loghandler:
# Only create a handler if we have channels to log to, and one if not self.loghandlers:
# doesn't already exist. # Only create handlers if they haven't already been set up.
self.loghandler = PyLinkChannelLogger(self, channels)
log.addHandler(self.loghandler) for channel, chandata in channels.items():
# Fetch the log level for this channel block.
level = None
if chandata is not None:
level = chandata.get('loglevel')
handler = PyLinkChannelLogger(self, channel, level=level)
self.loghandlers.append(handler)
log.addHandler(handler)
def initVars(self): def initVars(self):
""" """
@ -289,10 +297,9 @@ class Irc():
log.debug('(%s) _disconnect: Setting self.aborted to True.', self.name) log.debug('(%s) _disconnect: Setting self.aborted to True.', self.name)
self.aborted.set() self.aborted.set()
if self.loghandler is not None: log.debug('(%s) Removing channel logging handlers due to disconnect.', self.name)
log.debug('(%s) Removing channel logging handler due to disconnect.', self.name) while self.loghandlers:
log.removeHandler(self.loghandler) log.removeHandler(self.loghandlers.pop())
self.loghandler = None
try: try:
log.debug('(%s) _disconnect: Shutting down and closing socket.', self.name) log.debug('(%s) _disconnect: Shutting down and closing socket.', self.name)

View File

@ -44,11 +44,14 @@ logging:
loglevel: INFO loglevel: INFO
inspnet: inspnet:
- "#services" "#services":
- "#pylink-log" loglevel: INFO
"#pylink-notifications"
loglevel: WARNING
ts6net: ts6net:
- "#services" "#services":
loglevel: INFO
files: files:
# Logs to file targets. These will be placed in the log/ folder in the # Logs to file targets. These will be placed in the log/ folder in the
@ -58,12 +61,12 @@ logging:
# When running with ./pylink, this will create log/pylink-errors.log # When running with ./pylink, this will create log/pylink-errors.log
# When running with ./pylink someconf.yml, this will create log/someconf-errors.log # When running with ./pylink someconf.yml, this will create log/someconf-errors.log
- "errors" "errors":
loglevel: ERROR loglevel: ERROR
# Ditto above. When running with ./pylink, it will use log/pylink-commands.log # Ditto above. When running with ./pylink, it will use log/pylink-commands.log
# When running with ./pylink someconf.yml, this will create log/someconf-commands.log # When running with ./pylink someconf.yml, this will create log/someconf-commands.log
- "commands" "commands":
loglevel: INFO loglevel: INFO
servers: servers:

15
log.py
View File

@ -57,20 +57,24 @@ class PyLinkChannelLogger(logging.Handler):
""" """
Log handler to log to channels in PyLink. Log handler to log to channels in PyLink.
""" """
def __init__(self, irc, channels, level=None): def __init__(self, irc, channel, level=None):
super(PyLinkChannelLogger, self).__init__() super(PyLinkChannelLogger, self).__init__()
self.irc = irc self.irc = irc
self.channels = channels self.channel = channel
# Use a slightly simpler message formatter - logging to IRC doesn't need # Use a slightly simpler message formatter - logging to IRC doesn't need
# logging the time. # logging the time.
formatter = logging.Formatter('[%(levelname)s] %(message)s') formatter = logging.Formatter('[%(levelname)s] %(message)s')
self.setFormatter(formatter) self.setFormatter(formatter)
# HACK: Use setLevel twice to first coerse string log levels to ints,
# for easier comparison.
level = level or log.getEffectiveLevel()
self.setLevel(level)
# Log level has to be at least 20 (INFO) to prevent loops due # Log level has to be at least 20 (INFO) to prevent loops due
# to outgoing messages being logged # to outgoing messages being logged
level = level or log.getEffectiveLevel() loglevel = max(self.level, 20)
loglevel = max(level, 20)
self.setLevel(loglevel) self.setLevel(loglevel)
def emit(self, record): def emit(self, record):
@ -80,6 +84,5 @@ class PyLinkChannelLogger(logging.Handler):
# Only start logging if we're finished bursting # Only start logging if we're finished bursting
if hasattr(self.irc, 'pseudoclient') and self.irc.connected.is_set(): if hasattr(self.irc, 'pseudoclient') and self.irc.connected.is_set():
msg = self.format(record) msg = self.format(record)
for channel in self.channels: self.irc.msg(self.channel, msg)
self.irc.msg(channel, msg)

View File

@ -198,10 +198,11 @@ def rehash(irc, source, args):
ircobj.serverdata = new_conf['servers'][network] ircobj.serverdata = new_conf['servers'][network]
ircobj.botdata = new_conf['bot'] ircobj.botdata = new_conf['bot']
# Clear the IRC object's channel logger and replace it with a # Clear the IRC object's channel loggers and replace them with
# new one by re-running logSetup(). # new ones by re-running logSetup().
log.removeHandler(ircobj.loghandler) while ircobj.loghandlers:
ircobj.loghandler = None log.removeHandler(ircobj.loghandlers.pop())
ircobj.logSetup() ircobj.logSetup()
for network, sdata in new_conf['servers'].items(): for network, sdata in new_conf['servers'].items():