From 6bf1b14a36d1c3a4d3dfe229dca87ff84d993450 Mon Sep 17 00:00:00 2001 From: prash3r Date: Fri, 16 Sep 2022 16:45:33 +0200 Subject: [PATCH] notify clients on log events worse than INFO --- cbpi/controller/notification_controller.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cbpi/controller/notification_controller.py b/cbpi/controller/notification_controller.py index 5d2b4ef..26c25b4 100644 --- a/cbpi/controller/notification_controller.py +++ b/cbpi/controller/notification_controller.py @@ -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