From 1adfb0626e7777c6d29be65d4ad4ce2d57541301 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Wed, 28 Dec 2022 15:18:35 +0100 Subject: [PATCH] more structured logging --- __init__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/__init__.py b/__init__.py index ce20ae1..2ac9de1 100644 --- a/__init__.py +++ b/__init__.py @@ -39,6 +39,13 @@ except ImportError: logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) +def get_topic_logger(topic): + topic_logger = logger + for entry in topic.split('/'): + topic_logger = topic_logger.getChild(entry) + return topic_logger + + class mqtt_client(object): def __init__(self, name, host, port=1883, username=None, password=None): self.__block_add_callbacks__ = True @@ -65,8 +72,7 @@ class mqtt_client(object): self.__client__.subscribe(topic) def send(self, topic, payload): - logger.debug("Sending message with topic %s and payload %s", - topic, str(payload)) + get_topic_logger(topic).debug("Sending message with topic %s and payload %s", topic, str(payload)) self.__client__.publish(topic, payload) def __on_connect__(self, client, userdata, flags, rc): @@ -82,7 +88,7 @@ class mqtt_client(object): logger.warning("Disconnect with rc=%d", rc) def __receive__(self, client, userdata, message): - logger.debug("Received message with topic %s and payload %s", message.topic, str(message.payload)) + get_topic_logger(message.topic).debug("Received message with topic %s and payload %s", message.topic, str(message.payload)) for topic in self.__callbacks__.copy(): if topic.endswith('#'): if message.topic.startswith(topic[:-1]):