blakeblackshear.frigate/frigate/log.py

77 lines
2.2 KiB
Python
Raw Normal View History

2020-11-04 04:26:39 +01:00
# adapted from https://medium.com/@jonathonbao/python3-logging-with-multiprocessing-f51f460b8778
import logging
import threading
2020-12-04 13:59:03 +01:00
import os
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
2021-01-03 20:41:02 +01:00
from setproctitle import setproctitle
from collections import deque
2020-11-04 04:26:39 +01:00
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()
formatter = logging.Formatter(
"[%(asctime)s] %(name)-30s %(levelname)-8s: %(message)s", "%Y-%m-%d %H:%M:%S"
)
2020-11-04 04:26:39 +01:00
console_handler.setFormatter(formatter)
root.addHandler(console_handler)
root.setLevel(logging.INFO)
2021-02-17 14:23:32 +01:00
2020-11-04 04:26:39 +01:00
def root_configurer(queue):
h = handlers.QueueHandler(queue)
root = logging.getLogger()
root.addHandler(h)
root.setLevel(logging.INFO)
2021-02-17 14:23:32 +01:00
2020-11-30 02:29:32 +01:00
def log_process(log_queue):
2020-11-04 13:28:07 +01:00
threading.current_thread().name = f"logger"
2021-01-03 20:41:02 +01:00
setproctitle("frigate.logger")
2020-11-04 04:26:39 +01:00
listener_configurer()
while True:
2020-11-29 23:19:59 +01:00
try:
2020-11-30 02:29:32 +01:00
record = log_queue.get(timeout=5)
2021-05-18 07:52:08 +02:00
except (queue.Empty, KeyboardInterrupt):
2020-11-29 23:19:59 +01:00
continue
2020-11-04 04:26:39 +01:00
logger = logging.getLogger(record.name)
logger.handle(record)
2020-12-04 13:59:03 +01:00
2021-02-17 14:23:32 +01:00
2020-12-04 13:59:03 +01:00
# based on https://codereview.stackexchange.com/a/17959
class LogPipe(threading.Thread):
def __init__(self, log_name, level):
"""Setup the object with a logger and a loglevel
and start the thread
"""
threading.Thread.__init__(self)
self.daemon = False
self.logger = logging.getLogger(log_name)
self.level = level
self.deque = deque(maxlen=100)
2020-12-04 13:59:03 +01:00
self.fdRead, self.fdWrite = os.pipe()
self.pipeReader = os.fdopen(self.fdRead)
self.start()
def fileno(self):
2021-02-17 14:23:32 +01:00
"""Return the write file descriptor of the pipe"""
2020-12-04 13:59:03 +01:00
return self.fdWrite
def run(self):
2021-02-17 14:23:32 +01:00
"""Run the thread, logging everything."""
for line in iter(self.pipeReader.readline, ""):
self.deque.append(line.strip("\n"))
2020-12-04 13:59:03 +01:00
self.pipeReader.close()
2021-02-17 14:23:32 +01:00
def dump(self):
while len(self.deque) > 0:
self.logger.log(self.level, self.deque.popleft())
2020-12-04 13:59:03 +01:00
def close(self):
2021-02-17 14:23:32 +01:00
"""Close the write end of the pipe."""
2021-01-03 20:41:02 +01:00
os.close(self.fdWrite)