create LogHandler for the dashborad
- simple memory log handler which stores the last 64 warnings/errors for the dashboard
This commit is contained in:
24
app/src/web/log_handler.py
Normal file
24
app/src/web/log_handler.py
Normal 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:]
|
||||
Reference in New Issue
Block a user