2020-11-04 04:26:39 +01:00
|
|
|
# adapted from https://medium.com/@jonathonbao/python3-logging-with-multiprocessing-f51f460b8778
|
|
|
|
import logging
|
|
|
|
import threading
|
2020-11-29 23:19:59 +01:00
|
|
|
import signal
|
2020-11-30 02:29:32 +01:00
|
|
|
import queue
|
2020-11-29 23:19:59 +01:00
|
|
|
import multiprocessing as mp
|
2020-11-04 04:26:39 +01:00
|
|
|
from logging import handlers
|
|
|
|
|
2020-11-04 13:31:25 +01:00
|
|
|
|
2020-11-04 04:26:39 +01:00
|
|
|
def listener_configurer():
|
|
|
|
root = logging.getLogger()
|
|
|
|
console_handler = logging.StreamHandler()
|
2020-11-04 13:28:07 +01:00
|
|
|
formatter = logging.Formatter('%(threadName)-25s %(name)-16s %(levelname)-8s: %(message)s')
|
2020-11-04 04:26:39 +01:00
|
|
|
console_handler.setFormatter(formatter)
|
|
|
|
root.addHandler(console_handler)
|
|
|
|
root.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
def root_configurer(queue):
|
|
|
|
h = handlers.QueueHandler(queue)
|
|
|
|
root = logging.getLogger()
|
|
|
|
root.addHandler(h)
|
|
|
|
root.setLevel(logging.INFO)
|
|
|
|
|
2020-11-30 02:29:32 +01:00
|
|
|
def log_process(log_queue):
|
2020-11-29 23:19:59 +01:00
|
|
|
stop_event = mp.Event()
|
|
|
|
def receiveSignal(signalNumber, frame):
|
|
|
|
stop_event.set()
|
|
|
|
|
|
|
|
signal.signal(signal.SIGTERM, receiveSignal)
|
|
|
|
signal.signal(signal.SIGINT, receiveSignal)
|
|
|
|
|
2020-11-04 13:28:07 +01:00
|
|
|
threading.current_thread().name = f"logger"
|
2020-11-04 04:26:39 +01:00
|
|
|
listener_configurer()
|
|
|
|
while True:
|
2020-11-30 02:29:32 +01:00
|
|
|
if stop_event.is_set() and log_queue.empty():
|
2020-11-29 23:19:59 +01:00
|
|
|
break
|
|
|
|
try:
|
2020-11-30 02:29:32 +01:00
|
|
|
record = log_queue.get(timeout=5)
|
2020-11-29 23:19:59 +01:00
|
|
|
except queue.Empty:
|
|
|
|
continue
|
2020-11-04 04:26:39 +01:00
|
|
|
logger = logging.getLogger(record.name)
|
|
|
|
logger.handle(record)
|