create LogHandler for the dashborad

- simple memory log handler which stores the last
  64 warnings/errors for the dashboard
This commit is contained in:
Stefan Allius
2025-05-04 15:28:28 +02:00
parent ba025eb09b
commit 089c35f89e

View File

@@ -0,0 +1,24 @@
from logging import Handler
from logging import LogRecord
import logging
from collections import deque
from singleton import Singleton
class LogHandler(Handler, metaclass=Singleton):
def __init__(self, capacity=64):
super().__init__(logging.WARNING)
self.capacity = capacity
self.buffer = deque(maxlen=capacity)
def emit(self, record: LogRecord):
self.buffer.append({
'ctime': record.created,
'level': record.levelno,
'lname': record.levelname,
'msg': record.getMessage()
})
def get_buffer(self, elms=0) -> list:
return list(self.buffer)[-elms:]