mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-25 00:18:17 +01:00
notify clients on log events worse than INFO
This commit is contained in:
parent
ac3c880523
commit
6bf1b14a36
1 changed files with 15 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
|||
import asyncio
|
||||
from email import message
|
||||
from cbpi.api.dataclasses import NotificationType
|
||||
import logging
|
||||
import shortuuid
|
||||
|
@ -10,9 +11,23 @@ class NotificationController:
|
|||
'''
|
||||
self.cbpi = cbpi
|
||||
self.logger = logging.getLogger(__name__)
|
||||
logging.root.addFilter(self.notify_log_event)
|
||||
self.callback_cache = {}
|
||||
self.listener = {}
|
||||
|
||||
def notify_log_event(self, record):
|
||||
try:
|
||||
if record.levelno > 20:
|
||||
# on log events higher then INFO we want to notify all clients
|
||||
type = NotificationType.WARNING
|
||||
if record.levelno > 30:
|
||||
type = NotificationType.ERROR
|
||||
self.cbpi.notify(title=f"{record.levelname}", message=record.msg, type = type)
|
||||
except Exception as e:
|
||||
pass
|
||||
finally:
|
||||
return True
|
||||
|
||||
def add_listener(self, method):
|
||||
listener_id = shortuuid.uuid()
|
||||
self.listener[listener_id] = method
|
||||
|
|
Loading…
Reference in a new issue