From d085aa85814e0fb241ba20b932e7ec9549978d53 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Wed, 28 Dec 2022 15:34:30 +0100 Subject: [PATCH] more structured logging --- devices/__init__.py | 78 ++++++++++++++++++++++----------------------- mqtt | 2 +- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/devices/__init__.py b/devices/__init__.py index ec7d3c5..ff60f3c 100644 --- a/devices/__init__.py +++ b/devices/__init__.py @@ -35,7 +35,6 @@ try: from config import APP_NAME as ROOT_LOGGER_NAME except ImportError: ROOT_LOGGER_NAME = 'root' -logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) BATTERY_WARN_LEVEL = 5 @@ -65,6 +64,9 @@ class base(dict): # data storage self.mqtt_client = mqtt_client self.topic = topic + self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) + for entry in self.topic.split('/'): + self.logger = self.logger.getChild(entry) # initialisations dict.__init__(self) mqtt_client.add_callback( @@ -94,17 +96,17 @@ class base(dict): self.__previous__[key] = prev_value # Filter, if needed self.unpack_filter(key) - logger.debug("Received data for (%s) %s - %s", self.topic, key, str(self.get(key))) + self.logger.debug("Received data for (%s) %s - %s", self.topic, key, str(self.get(key))) self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key)) elif key not in self.RX_IGNORE_KEYS: - logger.warning('Got a message from \"%s\" with unparsed content "%s"', self.topic, key) + self.logger.warning('Got a message from \"%s\" with unparsed content "%s"', self.topic, key) else: - logger.debug("Ignoring key %s", key) + self.logger.debug("Ignoring key %s", key) def unpack(self, message): content_key = message.topic[len(self.topic) + 1:] if content_key not in self.RX_IGNORE_TOPICS and (not message.topic.endswith(self.TX_TOPIC) or len(self.TX_TOPIC) == 0): - logger.debug("Unpacking content_key \"%s\" from message.", content_key) + self.logger.debug("Unpacking content_key \"%s\" from message.", content_key) if is_json(message.payload): data = json.loads(message.payload) if type(data) is dict: @@ -118,7 +120,7 @@ class base(dict): content_key, message.payload.decode('utf-8')) self.warning_caller() else: - logger.debug("Ignoring topic %s", content_key) + self.logger.debug("Ignoring topic %s", content_key) def pack_filter(self, key, data): if key in self.TX_FILTER_DATA_KEYS: @@ -137,10 +139,9 @@ class base(dict): data = self.pack_filter(key, data) if self.TX_TOPIC is not None: if self.TX_TYPE < 0: - logger.error( - "Unknown tx type. Set TX_TYPE of class to a known value") + self.logger.error("Unknown tx type. Set TX_TYPE of class to a known value") else: - logger.debug("Sending data for (%s) %s - %s", self.topic, key, str(data)) + self.logger.debug("Sending data for (%s) %s - %s", self.topic, key, str(data)) if self.TX_TYPE == self.TX_DICT: self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data})) else: @@ -148,8 +149,7 @@ class base(dict): data = json.dumps(data) self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data) else: - logger.error( - "Unknown tx toptic. Set TX_TOPIC of class to a known value") + self.logger.error("Unknown tx toptic. Set TX_TOPIC of class to a known value") def add_callback(self, key, data, callback, on_change_only=False): """ @@ -175,7 +175,7 @@ class base(dict): def warning_caller(self): if self.warning_call_condition(): warn_txt = self.warning_text() - logger.warning(warn_txt) + self.logger.warning(warn_txt) if self.warning_callback is not None: self.warning_callback(self, warn_txt) @@ -274,11 +274,11 @@ class shelly(base): self.pack(self.KEY_OUTPUT_0, state) def set_output_0_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) self.set_output_0(data) def toggle_output_0_mcb(self, device, key, data): - logger.info("%s: Toggeling output 0", self.topic) + self.logger.info("%s: Toggeling output 0", self.topic) self.set_output_0('toggle') def set_output_1(self, state): @@ -286,11 +286,11 @@ class shelly(base): self.pack(self.KEY_OUTPUT_1, state) def set_output_1_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data)) self.set_output_1(data) def toggle_output_1_mcb(self, device, key, data): - logger.info("%s: Toggeling output 1", self.topic) + self.logger.info("%s: Toggeling output 1", self.topic) self.set_output_1('toggle') @@ -328,11 +328,11 @@ class silvercrest_powerplug(base): self.pack(self.KEY_OUTPUT_0, state) def set_output_0_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) self.set_output_0(data) def toggle_output_0_mcb(self, device, key, data): - logger.info("%s: Toggeling output 0", self.topic) + self.logger.info("%s: Toggeling output 0", self.topic) self.set_output_0('toggle') @@ -417,11 +417,11 @@ class my_powerplug(base): self.pack(self.KEY_OUTPUT_0, state) def set_output_0_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) self.set_output_0(data) def toggle_output_0_mcb(self, device, key, data): - logger.info("%s: Toggeling output 0", self.topic) + self.logger.info("%s: Toggeling output 0", self.topic) self.set_output_0('toggle') def set_output_1(self, state): @@ -429,11 +429,11 @@ class my_powerplug(base): self.pack(self.KEY_OUTPUT_1, state) def set_output_1_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data)) self.set_output_1(data) def toggle_output_1_mcb(self, device, key, data): - logger.info("%s: Toggeling output 1", self.topic) + self.logger.info("%s: Toggeling output 1", self.topic) self.set_output_1('toggle') def set_output_2(self, state): @@ -441,11 +441,11 @@ class my_powerplug(base): self.pack(self.KEY_OUTPUT_2, state) def set_output_2_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "%s: Changing output 2 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "%s: Changing output 2 to %s", self.topic, str(data)) self.set_output_2(data) def toggle_output_2_mcb(self, device, key, data): - logger.info("%s: Toggeling output 2", self.topic) + self.logger.info("%s: Toggeling output 2", self.topic) self.set_output_2('toggle') def set_output_3(self, state): @@ -453,11 +453,11 @@ class my_powerplug(base): self.pack(self.KEY_OUTPUT_3, state) def set_output_3_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "%s: Changing output 3 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "%s: Changing output 3 to %s", self.topic, str(data)) self.set_output_3(data) def toggle_output_3_mcb(self, device, key, data): - logger.info("%s: Toggeling output 3", self.topic) + self.logger.info("%s: Toggeling output 3", self.topic) self.set_output_3('toggle') def set_output_all(self, state): @@ -465,11 +465,11 @@ class my_powerplug(base): self.pack(self.KEY_OUTPUT_ALL, state) def set_output_all_mcb(self, device, key, data): - logger.info("%s: Changing all outputs to %s", self.topic, str(data)) + self.logger.info("%s: Changing all outputs to %s", self.topic, str(data)) self.set_output_all(data) def toggle_output_all_mcb(self, device, key, data): - logger.info("%s: Toggeling all outputs", self.topic) + self.logger.info("%s: Toggeling all outputs", self.topic) self.set_output_0('toggle') @@ -537,11 +537,11 @@ class tradfri_light(base): self.pack(self.KEY_OUTPUT_0, state) def set_output_0_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data)) self.set_output_0(data) def toggle_output_0_mcb(self, device, key, data): - logger.info("%s: Toggeling output 0", self.topic) + self.logger.info("%s: Toggeling output 0", self.topic) self.set_output_0('toggle') def set_brightness(self, brightness): @@ -549,7 +549,7 @@ class tradfri_light(base): self.pack(self.KEY_BRIGHTNESS, brightness) def set_brightness_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data)) self.set_brightness(data) def default_inc(self, speed=40): @@ -566,7 +566,7 @@ class tradfri_light(base): self.pack(self.KEY_COLOR_TEMP, color_temp) def set_color_temp_mcb(self, device, key, data): - logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "%s: Changing color temperature to %s", self.topic, str(data)) + self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "%s: Changing color temperature to %s", self.topic, str(data)) self.set_color_temp(data) @@ -696,7 +696,7 @@ class nodered_gui_switch(nodered_gui_button): self.pack(self.KEY_STATE, data) def set_state_mcb(self, device, key, data): - logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) + self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) self.set_state(data) @@ -735,7 +735,7 @@ class nodered_gui_brightness_color_temp(base): self.pack(self.KEY_ENABLE, data) def set_enable_mcb(self, device, key, data): - logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) + self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) self.set_enable(data) def set_brightness(self, data): @@ -743,7 +743,7 @@ class nodered_gui_brightness_color_temp(base): self.pack(self.KEY_BRIGHTNESS, data) def set_brightness_mcb(self, device, key, data): - logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) + self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) self.set_brightness(data) def set_color_temp(self, data): @@ -751,7 +751,7 @@ class nodered_gui_brightness_color_temp(base): self.pack(self.KEY_COLOR_TEMP, data) def set_color_temp_mcb(self, device, key, data): - logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) + self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) self.set_color_temp(data) @@ -773,7 +773,7 @@ class nodered_gui_leds(base): def set_led(self, key, data): """data: [True, False]""" - logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) + self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data)) self.pack(key, data) @@ -828,7 +828,7 @@ class brennenstuhl_heatingvalve(base): self.pack(self.KEY_HEATING_SETPOINT, setpoint) def set_heating_setpoint_mcb(self, device, key, data): - logger.info("%s: Changing heating setpoint to %s", self.topic, str(data)) + self.logger.info("%s: Changing heating setpoint to %s", self.topic, str(data)) self.set_heating_setpoint(data) @@ -891,7 +891,7 @@ class status(base): self.pack(self.KEY_STATE + "/" + str(num), data) def set_state_mcb(self, device, key, data): - logger.info("%s: Changing state to %s", self.topic, str(data)) + self.logger.info("%s: Changing state to %s", self.topic, str(data)) self.set_state(data) diff --git a/mqtt b/mqtt index 79ac04f..1adfb06 160000 --- a/mqtt +++ b/mqtt @@ -1 +1 @@ -Subproject commit 79ac04ffdb61ea61334b2bb90bee565472957657 +Subproject commit 1adfb0626e7777c6d29be65d4ad4ce2d57541301