more structured logging

This commit is contained in:
Dirk Alders 2022-12-28 15:18:35 +01:00
parent 79ac04ffdb
commit 1adfb0626e

View File

@ -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]):