Kaynağa Gözat

more structured logging

tags/v1.0.0
Dirk Alders 2 yıl önce
ebeveyn
işleme
d085aa8581
2 değiştirilmiş dosya ile 40 ekleme ve 40 silme
  1. 39
    39
      devices/__init__.py
  2. 1
    1
      mqtt

+ 39
- 39
devices/__init__.py Dosyayı Görüntüle

35
     from config import APP_NAME as ROOT_LOGGER_NAME
35
     from config import APP_NAME as ROOT_LOGGER_NAME
36
 except ImportError:
36
 except ImportError:
37
     ROOT_LOGGER_NAME = 'root'
37
     ROOT_LOGGER_NAME = 'root'
38
-logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
39
 
38
 
40
 BATTERY_WARN_LEVEL = 5
39
 BATTERY_WARN_LEVEL = 5
41
 
40
 
65
         # data storage
64
         # data storage
66
         self.mqtt_client = mqtt_client
65
         self.mqtt_client = mqtt_client
67
         self.topic = topic
66
         self.topic = topic
67
+        self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
68
+        for entry in self.topic.split('/'):
69
+            self.logger = self.logger.getChild(entry)
68
         # initialisations
70
         # initialisations
69
         dict.__init__(self)
71
         dict.__init__(self)
70
         mqtt_client.add_callback(
72
         mqtt_client.add_callback(
94
             self.__previous__[key] = prev_value
96
             self.__previous__[key] = prev_value
95
             # Filter, if needed
97
             # Filter, if needed
96
             self.unpack_filter(key)
98
             self.unpack_filter(key)
97
-            logger.debug("Received data for (%s) %s - %s", self.topic, key, str(self.get(key)))
99
+            self.logger.debug("Received data for (%s) %s - %s", self.topic, key, str(self.get(key)))
98
             self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key))
100
             self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key))
99
         elif key not in self.RX_IGNORE_KEYS:
101
         elif key not in self.RX_IGNORE_KEYS:
100
-            logger.warning('Got a message from \"%s\" with unparsed content "%s"', self.topic, key)
102
+            self.logger.warning('Got a message from \"%s\" with unparsed content "%s"', self.topic, key)
101
         else:
103
         else:
102
-            logger.debug("Ignoring key %s", key)
104
+            self.logger.debug("Ignoring key %s", key)
103
 
105
 
104
     def unpack(self, message):
106
     def unpack(self, message):
105
         content_key = message.topic[len(self.topic) + 1:]
107
         content_key = message.topic[len(self.topic) + 1:]
106
         if content_key not in self.RX_IGNORE_TOPICS and (not message.topic.endswith(self.TX_TOPIC) or len(self.TX_TOPIC) == 0):
108
         if content_key not in self.RX_IGNORE_TOPICS and (not message.topic.endswith(self.TX_TOPIC) or len(self.TX_TOPIC) == 0):
107
-            logger.debug("Unpacking content_key \"%s\" from message.", content_key)
109
+            self.logger.debug("Unpacking content_key \"%s\" from message.", content_key)
108
             if is_json(message.payload):
110
             if is_json(message.payload):
109
                 data = json.loads(message.payload)
111
                 data = json.loads(message.payload)
110
                 if type(data) is dict:
112
                 if type(data) is dict:
118
                     content_key, message.payload.decode('utf-8'))
120
                     content_key, message.payload.decode('utf-8'))
119
             self.warning_caller()
121
             self.warning_caller()
120
         else:
122
         else:
121
-            logger.debug("Ignoring topic %s", content_key)
123
+            self.logger.debug("Ignoring topic %s", content_key)
122
 
124
 
123
     def pack_filter(self, key, data):
125
     def pack_filter(self, key, data):
124
         if key in self.TX_FILTER_DATA_KEYS:
126
         if key in self.TX_FILTER_DATA_KEYS:
137
         data = self.pack_filter(key, data)
139
         data = self.pack_filter(key, data)
138
         if self.TX_TOPIC is not None:
140
         if self.TX_TOPIC is not None:
139
             if self.TX_TYPE < 0:
141
             if self.TX_TYPE < 0:
140
-                logger.error(
141
-                    "Unknown tx type. Set TX_TYPE of class to a known value")
142
+                self.logger.error("Unknown tx type. Set TX_TYPE of class to a known value")
142
             else:
143
             else:
143
-                logger.debug("Sending data for (%s) %s - %s", self.topic, key, str(data))
144
+                self.logger.debug("Sending data for (%s) %s - %s", self.topic, key, str(data))
144
                 if self.TX_TYPE == self.TX_DICT:
145
                 if self.TX_TYPE == self.TX_DICT:
145
                     self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
146
                     self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
146
                 else:
147
                 else:
148
                         data = json.dumps(data)
149
                         data = json.dumps(data)
149
                     self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
150
                     self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
150
         else:
151
         else:
151
-            logger.error(
152
-                "Unknown tx toptic. Set TX_TOPIC of class to a known value")
152
+            self.logger.error("Unknown tx toptic. Set TX_TOPIC of class to a known value")
153
 
153
 
154
     def add_callback(self, key, data, callback, on_change_only=False):
154
     def add_callback(self, key, data, callback, on_change_only=False):
155
         """
155
         """
175
     def warning_caller(self):
175
     def warning_caller(self):
176
         if self.warning_call_condition():
176
         if self.warning_call_condition():
177
             warn_txt = self.warning_text()
177
             warn_txt = self.warning_text()
178
-            logger.warning(warn_txt)
178
+            self.logger.warning(warn_txt)
179
             if self.warning_callback is not None:
179
             if self.warning_callback is not None:
180
                 self.warning_callback(self, warn_txt)
180
                 self.warning_callback(self, warn_txt)
181
 
181
 
274
         self.pack(self.KEY_OUTPUT_0, state)
274
         self.pack(self.KEY_OUTPUT_0, state)
275
 
275
 
276
     def set_output_0_mcb(self, device, key, data):
276
     def set_output_0_mcb(self, device, key, data):
277
-        logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
277
+        self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
278
         self.set_output_0(data)
278
         self.set_output_0(data)
279
 
279
 
280
     def toggle_output_0_mcb(self, device, key, data):
280
     def toggle_output_0_mcb(self, device, key, data):
281
-        logger.info("%s: Toggeling output 0", self.topic)
281
+        self.logger.info("%s: Toggeling output 0", self.topic)
282
         self.set_output_0('toggle')
282
         self.set_output_0('toggle')
283
 
283
 
284
     def set_output_1(self, state):
284
     def set_output_1(self, state):
286
         self.pack(self.KEY_OUTPUT_1, state)
286
         self.pack(self.KEY_OUTPUT_1, state)
287
 
287
 
288
     def set_output_1_mcb(self, device, key, data):
288
     def set_output_1_mcb(self, device, key, data):
289
-        logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
289
+        self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
290
         self.set_output_1(data)
290
         self.set_output_1(data)
291
 
291
 
292
     def toggle_output_1_mcb(self, device, key, data):
292
     def toggle_output_1_mcb(self, device, key, data):
293
-        logger.info("%s: Toggeling output 1", self.topic)
293
+        self.logger.info("%s: Toggeling output 1", self.topic)
294
         self.set_output_1('toggle')
294
         self.set_output_1('toggle')
295
 
295
 
296
 
296
 
328
         self.pack(self.KEY_OUTPUT_0, state)
328
         self.pack(self.KEY_OUTPUT_0, state)
329
 
329
 
330
     def set_output_0_mcb(self, device, key, data):
330
     def set_output_0_mcb(self, device, key, data):
331
-        logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
331
+        self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
332
         self.set_output_0(data)
332
         self.set_output_0(data)
333
 
333
 
334
     def toggle_output_0_mcb(self, device, key, data):
334
     def toggle_output_0_mcb(self, device, key, data):
335
-        logger.info("%s: Toggeling output 0", self.topic)
335
+        self.logger.info("%s: Toggeling output 0", self.topic)
336
         self.set_output_0('toggle')
336
         self.set_output_0('toggle')
337
 
337
 
338
 
338
 
417
         self.pack(self.KEY_OUTPUT_0, state)
417
         self.pack(self.KEY_OUTPUT_0, state)
418
 
418
 
419
     def set_output_0_mcb(self, device, key, data):
419
     def set_output_0_mcb(self, device, key, data):
420
-        logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
420
+        self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
421
         self.set_output_0(data)
421
         self.set_output_0(data)
422
 
422
 
423
     def toggle_output_0_mcb(self, device, key, data):
423
     def toggle_output_0_mcb(self, device, key, data):
424
-        logger.info("%s: Toggeling output 0", self.topic)
424
+        self.logger.info("%s: Toggeling output 0", self.topic)
425
         self.set_output_0('toggle')
425
         self.set_output_0('toggle')
426
 
426
 
427
     def set_output_1(self, state):
427
     def set_output_1(self, state):
429
         self.pack(self.KEY_OUTPUT_1, state)
429
         self.pack(self.KEY_OUTPUT_1, state)
430
 
430
 
431
     def set_output_1_mcb(self, device, key, data):
431
     def set_output_1_mcb(self, device, key, data):
432
-        logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
432
+        self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
433
         self.set_output_1(data)
433
         self.set_output_1(data)
434
 
434
 
435
     def toggle_output_1_mcb(self, device, key, data):
435
     def toggle_output_1_mcb(self, device, key, data):
436
-        logger.info("%s: Toggeling output 1", self.topic)
436
+        self.logger.info("%s: Toggeling output 1", self.topic)
437
         self.set_output_1('toggle')
437
         self.set_output_1('toggle')
438
 
438
 
439
     def set_output_2(self, state):
439
     def set_output_2(self, state):
441
         self.pack(self.KEY_OUTPUT_2, state)
441
         self.pack(self.KEY_OUTPUT_2, state)
442
 
442
 
443
     def set_output_2_mcb(self, device, key, data):
443
     def set_output_2_mcb(self, device, key, data):
444
-        logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "%s: Changing output 2 to %s", self.topic, str(data))
444
+        self.logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "%s: Changing output 2 to %s", self.topic, str(data))
445
         self.set_output_2(data)
445
         self.set_output_2(data)
446
 
446
 
447
     def toggle_output_2_mcb(self, device, key, data):
447
     def toggle_output_2_mcb(self, device, key, data):
448
-        logger.info("%s: Toggeling output 2", self.topic)
448
+        self.logger.info("%s: Toggeling output 2", self.topic)
449
         self.set_output_2('toggle')
449
         self.set_output_2('toggle')
450
 
450
 
451
     def set_output_3(self, state):
451
     def set_output_3(self, state):
453
         self.pack(self.KEY_OUTPUT_3, state)
453
         self.pack(self.KEY_OUTPUT_3, state)
454
 
454
 
455
     def set_output_3_mcb(self, device, key, data):
455
     def set_output_3_mcb(self, device, key, data):
456
-        logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "%s: Changing output 3 to %s", self.topic, str(data))
456
+        self.logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "%s: Changing output 3 to %s", self.topic, str(data))
457
         self.set_output_3(data)
457
         self.set_output_3(data)
458
 
458
 
459
     def toggle_output_3_mcb(self, device, key, data):
459
     def toggle_output_3_mcb(self, device, key, data):
460
-        logger.info("%s: Toggeling output 3", self.topic)
460
+        self.logger.info("%s: Toggeling output 3", self.topic)
461
         self.set_output_3('toggle')
461
         self.set_output_3('toggle')
462
 
462
 
463
     def set_output_all(self, state):
463
     def set_output_all(self, state):
465
         self.pack(self.KEY_OUTPUT_ALL, state)
465
         self.pack(self.KEY_OUTPUT_ALL, state)
466
 
466
 
467
     def set_output_all_mcb(self, device, key, data):
467
     def set_output_all_mcb(self, device, key, data):
468
-        logger.info("%s: Changing all outputs to %s", self.topic, str(data))
468
+        self.logger.info("%s: Changing all outputs to %s", self.topic, str(data))
469
         self.set_output_all(data)
469
         self.set_output_all(data)
470
 
470
 
471
     def toggle_output_all_mcb(self, device, key, data):
471
     def toggle_output_all_mcb(self, device, key, data):
472
-        logger.info("%s: Toggeling all outputs", self.topic)
472
+        self.logger.info("%s: Toggeling all outputs", self.topic)
473
         self.set_output_0('toggle')
473
         self.set_output_0('toggle')
474
 
474
 
475
 
475
 
537
         self.pack(self.KEY_OUTPUT_0, state)
537
         self.pack(self.KEY_OUTPUT_0, state)
538
 
538
 
539
     def set_output_0_mcb(self, device, key, data):
539
     def set_output_0_mcb(self, device, key, data):
540
-        logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
540
+        self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
541
         self.set_output_0(data)
541
         self.set_output_0(data)
542
 
542
 
543
     def toggle_output_0_mcb(self, device, key, data):
543
     def toggle_output_0_mcb(self, device, key, data):
544
-        logger.info("%s: Toggeling output 0", self.topic)
544
+        self.logger.info("%s: Toggeling output 0", self.topic)
545
         self.set_output_0('toggle')
545
         self.set_output_0('toggle')
546
 
546
 
547
     def set_brightness(self, brightness):
547
     def set_brightness(self, brightness):
549
         self.pack(self.KEY_BRIGHTNESS, brightness)
549
         self.pack(self.KEY_BRIGHTNESS, brightness)
550
 
550
 
551
     def set_brightness_mcb(self, device, key, data):
551
     def set_brightness_mcb(self, device, key, data):
552
-        logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data))
552
+        self.logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data))
553
         self.set_brightness(data)
553
         self.set_brightness(data)
554
 
554
 
555
     def default_inc(self, speed=40):
555
     def default_inc(self, speed=40):
566
         self.pack(self.KEY_COLOR_TEMP, color_temp)
566
         self.pack(self.KEY_COLOR_TEMP, color_temp)
567
 
567
 
568
     def set_color_temp_mcb(self, device, key, data):
568
     def set_color_temp_mcb(self, device, key, data):
569
-        logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "%s: Changing color temperature to %s", self.topic, str(data))
569
+        self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "%s: Changing color temperature to %s", self.topic, str(data))
570
         self.set_color_temp(data)
570
         self.set_color_temp(data)
571
 
571
 
572
 
572
 
696
         self.pack(self.KEY_STATE, data)
696
         self.pack(self.KEY_STATE, data)
697
 
697
 
698
     def set_state_mcb(self, device, key, data):
698
     def set_state_mcb(self, device, key, data):
699
-        logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
699
+        self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
700
         self.set_state(data)
700
         self.set_state(data)
701
 
701
 
702
 
702
 
735
         self.pack(self.KEY_ENABLE, data)
735
         self.pack(self.KEY_ENABLE, data)
736
 
736
 
737
     def set_enable_mcb(self, device, key, data):
737
     def set_enable_mcb(self, device, key, data):
738
-        logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
738
+        self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
739
         self.set_enable(data)
739
         self.set_enable(data)
740
 
740
 
741
     def set_brightness(self, data):
741
     def set_brightness(self, data):
743
         self.pack(self.KEY_BRIGHTNESS, data)
743
         self.pack(self.KEY_BRIGHTNESS, data)
744
 
744
 
745
     def set_brightness_mcb(self, device, key, data):
745
     def set_brightness_mcb(self, device, key, data):
746
-        logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
746
+        self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
747
         self.set_brightness(data)
747
         self.set_brightness(data)
748
 
748
 
749
     def set_color_temp(self, data):
749
     def set_color_temp(self, data):
751
         self.pack(self.KEY_COLOR_TEMP, data)
751
         self.pack(self.KEY_COLOR_TEMP, data)
752
 
752
 
753
     def set_color_temp_mcb(self, device, key, data):
753
     def set_color_temp_mcb(self, device, key, data):
754
-        logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
754
+        self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
755
         self.set_color_temp(data)
755
         self.set_color_temp(data)
756
 
756
 
757
 
757
 
773
 
773
 
774
     def set_led(self, key, data):
774
     def set_led(self, key, data):
775
         """data: [True, False]"""
775
         """data: [True, False]"""
776
-        logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
776
+        self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
777
         self.pack(key, data)
777
         self.pack(key, data)
778
 
778
 
779
 
779
 
828
         self.pack(self.KEY_HEATING_SETPOINT, setpoint)
828
         self.pack(self.KEY_HEATING_SETPOINT, setpoint)
829
 
829
 
830
     def set_heating_setpoint_mcb(self, device, key, data):
830
     def set_heating_setpoint_mcb(self, device, key, data):
831
-        logger.info("%s: Changing heating setpoint to %s", self.topic, str(data))
831
+        self.logger.info("%s: Changing heating setpoint to %s", self.topic, str(data))
832
         self.set_heating_setpoint(data)
832
         self.set_heating_setpoint(data)
833
 
833
 
834
 
834
 
891
         self.pack(self.KEY_STATE + "/" + str(num), data)
891
         self.pack(self.KEY_STATE + "/" + str(num), data)
892
 
892
 
893
     def set_state_mcb(self, device, key, data):
893
     def set_state_mcb(self, device, key, data):
894
-        logger.info("%s: Changing state to %s", self.topic, str(data))
894
+        self.logger.info("%s: Changing state to %s", self.topic, str(data))
895
         self.set_state(data)
895
         self.set_state(data)
896
 
896
 
897
 
897
 

+ 1
- 1
mqtt

1
-Subproject commit 79ac04ffdb61ea61334b2bb90bee565472957657
1
+Subproject commit 1adfb0626e7777c6d29be65d4ad4ce2d57541301

Loading…
İptal
Kaydet