2016-11-12 02:22:39 +01:00
|
|
|
# Code to implement asynchronous logging from a background thread
|
|
|
|
#
|
2017-04-14 15:58:34 +02:00
|
|
|
# Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
|
2016-11-12 02:22:39 +01:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2017-05-01 19:44:06 +02:00
|
|
|
import logging, logging.handlers, threading, Queue, time
|
2016-11-12 02:22:39 +01:00
|
|
|
|
|
|
|
# Class to forward all messages through a queue to a background thread
|
|
|
|
class QueueHandler(logging.Handler):
|
|
|
|
def __init__(self, queue):
|
|
|
|
logging.Handler.__init__(self)
|
|
|
|
self.queue = queue
|
|
|
|
def emit(self, record):
|
|
|
|
try:
|
|
|
|
self.format(record)
|
|
|
|
record.msg = record.message
|
|
|
|
record.args = None
|
|
|
|
record.exc_info = None
|
|
|
|
self.queue.put_nowait(record)
|
|
|
|
except Exception:
|
|
|
|
self.handleError(record)
|
|
|
|
|
|
|
|
# Class to poll a queue in a background thread and log each message
|
2017-05-01 19:44:06 +02:00
|
|
|
class QueueListener(logging.handlers.TimedRotatingFileHandler):
|
|
|
|
def __init__(self, filename):
|
|
|
|
logging.handlers.TimedRotatingFileHandler.__init__(
|
|
|
|
self, filename, when='midnight', backupCount=5)
|
|
|
|
self.bg_queue = Queue.Queue()
|
|
|
|
self.bg_thread = threading.Thread(target=self._bg_thread)
|
|
|
|
self.bg_thread.start()
|
|
|
|
self.rollover_info = {}
|
2016-11-12 02:22:39 +01:00
|
|
|
def _bg_thread(self):
|
|
|
|
while 1:
|
2017-05-01 19:44:06 +02:00
|
|
|
record = self.bg_queue.get(True)
|
2016-11-12 02:22:39 +01:00
|
|
|
if record is None:
|
|
|
|
break
|
2017-05-01 19:44:06 +02:00
|
|
|
self.handle(record)
|
2016-11-12 02:22:39 +01:00
|
|
|
def stop(self):
|
2017-05-01 19:44:06 +02:00
|
|
|
self.bg_queue.put_nowait(None)
|
|
|
|
self.bg_thread.join()
|
|
|
|
def set_rollover_info(self, name, info):
|
|
|
|
self.rollover_info[name] = info
|
|
|
|
def doRollover(self):
|
|
|
|
logging.handlers.TimedRotatingFileHandler.doRollover(self)
|
|
|
|
lines = [self.rollover_info[name]
|
|
|
|
for name in sorted(self.rollover_info)
|
|
|
|
if self.rollover_info[name]]
|
|
|
|
lines.append(
|
|
|
|
"=============== Log rollover at %s ===============" % (
|
|
|
|
time.asctime(),))
|
|
|
|
self.emit(logging.makeLogRecord(
|
|
|
|
{'msg': "\n".join(lines), 'level': logging.INFO}))
|
2016-11-12 02:22:39 +01:00
|
|
|
|
|
|
|
def setup_bg_logging(filename, debuglevel):
|
2017-05-01 19:44:06 +02:00
|
|
|
ql = QueueListener(filename)
|
|
|
|
qh = QueueHandler(ql.bg_queue)
|
2016-11-12 02:22:39 +01:00
|
|
|
root = logging.getLogger()
|
|
|
|
root.addHandler(qh)
|
|
|
|
root.setLevel(debuglevel)
|
|
|
|
return ql
|