소스 검색

more structured logging

tags/v1.0.0
Dirk Alders 2 년 전
부모
커밋
d085aa8581
2개의 변경된 파일40개의 추가작업 그리고 40개의 파일을 삭제
  1. 39
    39
      devices/__init__.py
  2. 1
    1
      mqtt

+ 39
- 39
devices/__init__.py 파일 보기

@@ -35,7 +35,6 @@ try:
35 35
     from config import APP_NAME as ROOT_LOGGER_NAME
36 36
 except ImportError:
37 37
     ROOT_LOGGER_NAME = 'root'
38
-logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
39 38
 
40 39
 BATTERY_WARN_LEVEL = 5
41 40
 
@@ -65,6 +64,9 @@ class base(dict):
65 64
         # data storage
66 65
         self.mqtt_client = mqtt_client
67 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 70
         # initialisations
69 71
         dict.__init__(self)
70 72
         mqtt_client.add_callback(
@@ -94,17 +96,17 @@ class base(dict):
94 96
             self.__previous__[key] = prev_value
95 97
             # Filter, if needed
96 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 100
             self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key))
99 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 103
         else:
102
-            logger.debug("Ignoring key %s", key)
104
+            self.logger.debug("Ignoring key %s", key)
103 105
 
104 106
     def unpack(self, message):
105 107
         content_key = message.topic[len(self.topic) + 1:]
106 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 110
             if is_json(message.payload):
109 111
                 data = json.loads(message.payload)
110 112
                 if type(data) is dict:
@@ -118,7 +120,7 @@ class base(dict):
118 120
                     content_key, message.payload.decode('utf-8'))
119 121
             self.warning_caller()
120 122
         else:
121
-            logger.debug("Ignoring topic %s", content_key)
123
+            self.logger.debug("Ignoring topic %s", content_key)
122 124
 
123 125
     def pack_filter(self, key, data):
124 126
         if key in self.TX_FILTER_DATA_KEYS:
@@ -137,10 +139,9 @@ class base(dict):
137 139
         data = self.pack_filter(key, data)
138 140
         if self.TX_TOPIC is not None:
139 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 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 145
                 if self.TX_TYPE == self.TX_DICT:
145 146
                     self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
146 147
                 else:
@@ -148,8 +149,7 @@ class base(dict):
148 149
                         data = json.dumps(data)
149 150
                     self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
150 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 154
     def add_callback(self, key, data, callback, on_change_only=False):
155 155
         """
@@ -175,7 +175,7 @@ class base(dict):
175 175
     def warning_caller(self):
176 176
         if self.warning_call_condition():
177 177
             warn_txt = self.warning_text()
178
-            logger.warning(warn_txt)
178
+            self.logger.warning(warn_txt)
179 179
             if self.warning_callback is not None:
180 180
                 self.warning_callback(self, warn_txt)
181 181
 
@@ -274,11 +274,11 @@ class shelly(base):
274 274
         self.pack(self.KEY_OUTPUT_0, state)
275 275
 
276 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 278
         self.set_output_0(data)
279 279
 
280 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 282
         self.set_output_0('toggle')
283 283
 
284 284
     def set_output_1(self, state):
@@ -286,11 +286,11 @@ class shelly(base):
286 286
         self.pack(self.KEY_OUTPUT_1, state)
287 287
 
288 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 290
         self.set_output_1(data)
291 291
 
292 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 294
         self.set_output_1('toggle')
295 295
 
296 296
 
@@ -328,11 +328,11 @@ class silvercrest_powerplug(base):
328 328
         self.pack(self.KEY_OUTPUT_0, state)
329 329
 
330 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 332
         self.set_output_0(data)
333 333
 
334 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 336
         self.set_output_0('toggle')
337 337
 
338 338
 
@@ -417,11 +417,11 @@ class my_powerplug(base):
417 417
         self.pack(self.KEY_OUTPUT_0, state)
418 418
 
419 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 421
         self.set_output_0(data)
422 422
 
423 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 425
         self.set_output_0('toggle')
426 426
 
427 427
     def set_output_1(self, state):
@@ -429,11 +429,11 @@ class my_powerplug(base):
429 429
         self.pack(self.KEY_OUTPUT_1, state)
430 430
 
431 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 433
         self.set_output_1(data)
434 434
 
435 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 437
         self.set_output_1('toggle')
438 438
 
439 439
     def set_output_2(self, state):
@@ -441,11 +441,11 @@ class my_powerplug(base):
441 441
         self.pack(self.KEY_OUTPUT_2, state)
442 442
 
443 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 445
         self.set_output_2(data)
446 446
 
447 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 449
         self.set_output_2('toggle')
450 450
 
451 451
     def set_output_3(self, state):
@@ -453,11 +453,11 @@ class my_powerplug(base):
453 453
         self.pack(self.KEY_OUTPUT_3, state)
454 454
 
455 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 457
         self.set_output_3(data)
458 458
 
459 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 461
         self.set_output_3('toggle')
462 462
 
463 463
     def set_output_all(self, state):
@@ -465,11 +465,11 @@ class my_powerplug(base):
465 465
         self.pack(self.KEY_OUTPUT_ALL, state)
466 466
 
467 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 469
         self.set_output_all(data)
470 470
 
471 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 473
         self.set_output_0('toggle')
474 474
 
475 475
 
@@ -537,11 +537,11 @@ class tradfri_light(base):
537 537
         self.pack(self.KEY_OUTPUT_0, state)
538 538
 
539 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 541
         self.set_output_0(data)
542 542
 
543 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 545
         self.set_output_0('toggle')
546 546
 
547 547
     def set_brightness(self, brightness):
@@ -549,7 +549,7 @@ class tradfri_light(base):
549 549
         self.pack(self.KEY_BRIGHTNESS, brightness)
550 550
 
551 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 553
         self.set_brightness(data)
554 554
 
555 555
     def default_inc(self, speed=40):
@@ -566,7 +566,7 @@ class tradfri_light(base):
566 566
         self.pack(self.KEY_COLOR_TEMP, color_temp)
567 567
 
568 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 570
         self.set_color_temp(data)
571 571
 
572 572
 
@@ -696,7 +696,7 @@ class nodered_gui_switch(nodered_gui_button):
696 696
         self.pack(self.KEY_STATE, data)
697 697
 
698 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 700
         self.set_state(data)
701 701
 
702 702
 
@@ -735,7 +735,7 @@ class nodered_gui_brightness_color_temp(base):
735 735
         self.pack(self.KEY_ENABLE, data)
736 736
 
737 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 739
         self.set_enable(data)
740 740
 
741 741
     def set_brightness(self, data):
@@ -743,7 +743,7 @@ class nodered_gui_brightness_color_temp(base):
743 743
         self.pack(self.KEY_BRIGHTNESS, data)
744 744
 
745 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 747
         self.set_brightness(data)
748 748
 
749 749
     def set_color_temp(self, data):
@@ -751,7 +751,7 @@ class nodered_gui_brightness_color_temp(base):
751 751
         self.pack(self.KEY_COLOR_TEMP, data)
752 752
 
753 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 755
         self.set_color_temp(data)
756 756
 
757 757
 
@@ -773,7 +773,7 @@ class nodered_gui_leds(base):
773 773
 
774 774
     def set_led(self, key, data):
775 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 777
         self.pack(key, data)
778 778
 
779 779
 
@@ -828,7 +828,7 @@ class brennenstuhl_heatingvalve(base):
828 828
         self.pack(self.KEY_HEATING_SETPOINT, setpoint)
829 829
 
830 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 832
         self.set_heating_setpoint(data)
833 833
 
834 834
 
@@ -891,7 +891,7 @@ class status(base):
891 891
         self.pack(self.KEY_STATE + "/" + str(num), data)
892 892
 
893 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 895
         self.set_state(data)
896 896
 
897 897
 

+ 1
- 1
mqtt

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

Loading…
취소
저장