Merge pull request #65 from prash3r/notify-log-events

notify clients on log events worse than INFO
This commit is contained in:
Alexander Vollkopf 2022-10-02 16:15:08 +02:00 committed by GitHub
commit 8c8d09ed72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
import asyncio import asyncio
from email import message
from cbpi.api.dataclasses import NotificationType from cbpi.api.dataclasses import NotificationType
import logging import logging
import shortuuid import shortuuid
@ -10,9 +11,23 @@ class NotificationController:
''' '''
self.cbpi = cbpi self.cbpi = cbpi
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
logging.root.addFilter(self.notify_log_event)
self.callback_cache = {} self.callback_cache = {}
self.listener = {} 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): def add_listener(self, method):
listener_id = shortuuid.uuid() listener_id = shortuuid.uuid()
self.listener[listener_id] = method self.listener[listener_id] = method