Browse Source

more structured logging

master
Dirk Alders 1 year ago
parent
commit
1adfb0626e
1 changed files with 9 additions and 3 deletions
  1. 9
    3
      __init__.py

+ 9
- 3
__init__.py View File

39
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
39
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
40
 
40
 
41
 
41
 
42
+def get_topic_logger(topic):
43
+    topic_logger = logger
44
+    for entry in topic.split('/'):
45
+        topic_logger = topic_logger.getChild(entry)
46
+    return topic_logger
47
+
48
+
42
 class mqtt_client(object):
49
 class mqtt_client(object):
43
     def __init__(self, name, host, port=1883, username=None, password=None):
50
     def __init__(self, name, host, port=1883, username=None, password=None):
44
         self.__block_add_callbacks__ = True
51
         self.__block_add_callbacks__ = True
65
         self.__client__.subscribe(topic)
72
         self.__client__.subscribe(topic)
66
 
73
 
67
     def send(self, topic, payload):
74
     def send(self, topic, payload):
68
-        logger.debug("Sending message with topic %s and payload %s",
69
-                     topic, str(payload))
75
+        get_topic_logger(topic).debug("Sending message with topic %s and payload %s", topic, str(payload))
70
         self.__client__.publish(topic, payload)
76
         self.__client__.publish(topic, payload)
71
 
77
 
72
     def __on_connect__(self, client, userdata, flags, rc):
78
     def __on_connect__(self, client, userdata, flags, rc):
82
         logger.warning("Disconnect with rc=%d", rc)
88
         logger.warning("Disconnect with rc=%d", rc)
83
 
89
 
84
     def __receive__(self, client, userdata, message):
90
     def __receive__(self, client, userdata, message):
85
-        logger.debug("Received message with topic %s and payload %s", message.topic, str(message.payload))
91
+        get_topic_logger(message.topic).debug("Received message with topic %s and payload %s", message.topic, str(message.payload))
86
         for topic in self.__callbacks__.copy():
92
         for topic in self.__callbacks__.copy():
87
             if topic.endswith('#'):
93
             if topic.endswith('#'):
88
                 if message.topic.startswith(topic[:-1]):
94
                 if message.topic.startswith(topic[:-1]):

Loading…
Cancel
Save