Kaynağa Gözat

more structured logging

master
Dirk Alders 1 yıl önce
ebeveyn
işleme
1adfb0626e
1 değiştirilmiş dosya ile 9 ekleme ve 3 silme
  1. 9
    3
      __init__.py

+ 9
- 3
__init__.py Dosyayı Görüntüle

@@ -39,6 +39,13 @@ except ImportError:
39 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 49
 class mqtt_client(object):
43 50
     def __init__(self, name, host, port=1883, username=None, password=None):
44 51
         self.__block_add_callbacks__ = True
@@ -65,8 +72,7 @@ class mqtt_client(object):
65 72
         self.__client__.subscribe(topic)
66 73
 
67 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 76
         self.__client__.publish(topic, payload)
71 77
 
72 78
     def __on_connect__(self, client, userdata, flags, rc):
@@ -82,7 +88,7 @@ class mqtt_client(object):
82 88
         logger.warning("Disconnect with rc=%d", rc)
83 89
 
84 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 92
         for topic in self.__callbacks__.copy():
87 93
             if topic.endswith('#'):
88 94
                 if message.topic.startswith(topic[:-1]):

Loading…
İptal
Kaydet