mirror of
https://github.com/jlu5/PyLink.git
synced 2024-12-02 15:59:26 +01:00
core: rehashable file loggers with log rotation support
Closes #176. Closes #315.
This commit is contained in:
parent
06ecc89603
commit
126a07bdf6
@ -5,7 +5,7 @@ import signal
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from pylinkirc import world, utils, conf, classes
|
from pylinkirc import world, utils, conf, classes
|
||||||
from pylinkirc.log import log
|
from pylinkirc.log import log, makeFileLogger, stopFileLoggers
|
||||||
|
|
||||||
def remove_network(ircobj):
|
def remove_network(ircobj):
|
||||||
"""Removes a network object from the pool."""
|
"""Removes a network object from the pool."""
|
||||||
@ -47,6 +47,14 @@ def _rehash():
|
|||||||
new_conf = conf.loadConf(fname, errors_fatal=False)
|
new_conf = conf.loadConf(fname, errors_fatal=False)
|
||||||
new_conf = conf.validateConf(new_conf)
|
new_conf = conf.validateConf(new_conf)
|
||||||
conf.conf = new_conf
|
conf.conf = new_conf
|
||||||
|
|
||||||
|
# Reset any file logger options.
|
||||||
|
stopFileLoggers()
|
||||||
|
files = new_conf['logging'].get('files')
|
||||||
|
if files:
|
||||||
|
for filename, config in files.items():
|
||||||
|
makeFileLogger(filename, config.get('loglevel'))
|
||||||
|
|
||||||
for network, ircobj in world.networkobjects.copy().items():
|
for network, ircobj in world.networkobjects.copy().items():
|
||||||
# Server was removed from the config file, disconnect them.
|
# Server was removed from the config file, disconnect them.
|
||||||
log.debug('rehash: checking if %r is in new conf still.', network)
|
log.debug('rehash: checking if %r is in new conf still.', network)
|
||||||
|
19
log.py
19
log.py
@ -7,12 +7,16 @@ access the global logger object by importing "log" from this module
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import logging.handlers
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from . import world
|
from . import world
|
||||||
from .conf import conf, confname
|
from .conf import conf, confname
|
||||||
|
|
||||||
|
# Stores a list of active file loggers.
|
||||||
|
fileloggers = []
|
||||||
|
|
||||||
stdout_level = conf['logging'].get('stdout') or 'INFO'
|
stdout_level = conf['logging'].get('stdout') or 'INFO'
|
||||||
|
|
||||||
logdir = os.path.join(os.getcwd(), 'log')
|
logdir = os.path.join(os.getcwd(), 'log')
|
||||||
@ -43,7 +47,8 @@ def makeFileLogger(filename, level=None):
|
|||||||
# PyLink instances from overwriting each others' log files.
|
# PyLink instances from overwriting each others' log files.
|
||||||
target = os.path.join(logdir, '%s-%s.log' % (confname, filename))
|
target = os.path.join(logdir, '%s-%s.log' % (confname, filename))
|
||||||
|
|
||||||
filelogger = logging.FileHandler(target, mode='w')
|
# TODO: configurable values here
|
||||||
|
filelogger = logging.handlers.RotatingFileHandler(target, maxBytes=52428800, backupCount=5)
|
||||||
filelogger.setFormatter(logformatter)
|
filelogger.setFormatter(logformatter)
|
||||||
|
|
||||||
# If no log level is specified, use the same one as STDOUT.
|
# If no log level is specified, use the same one as STDOUT.
|
||||||
@ -51,9 +56,21 @@ def makeFileLogger(filename, level=None):
|
|||||||
filelogger.setLevel(level)
|
filelogger.setLevel(level)
|
||||||
|
|
||||||
log.addHandler(filelogger)
|
log.addHandler(filelogger)
|
||||||
|
global fileloggers
|
||||||
|
fileloggers.append(filelogger)
|
||||||
|
|
||||||
return filelogger
|
return filelogger
|
||||||
|
|
||||||
|
def stopFileLoggers():
|
||||||
|
"""
|
||||||
|
De-initializes all file loggers.
|
||||||
|
"""
|
||||||
|
global fileloggers
|
||||||
|
for handler in fileloggers.copy():
|
||||||
|
handler.close()
|
||||||
|
log.removeHandler(handler)
|
||||||
|
fileloggers.remove(handler)
|
||||||
|
|
||||||
# Set up file logging now, creating a file logger for each block.
|
# Set up file logging now, creating a file logger for each block.
|
||||||
files = conf['logging'].get('files')
|
files = conf['logging'].get('files')
|
||||||
if files:
|
if files:
|
||||||
|
Loading…
Reference in New Issue
Block a user