more structured logging

This commit is contained in:
Dirk Alders 2022-12-28 15:34:30 +01:00
parent 8ba63349fb
commit d085aa8581
2 changed files with 40 additions and 40 deletions

View File

@ -35,7 +35,6 @@ try:
from config import APP_NAME as ROOT_LOGGER_NAME from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError: except ImportError:
ROOT_LOGGER_NAME = 'root' ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
BATTERY_WARN_LEVEL = 5 BATTERY_WARN_LEVEL = 5
@ -65,6 +64,9 @@ class base(dict):
# data storage # data storage
self.mqtt_client = mqtt_client self.mqtt_client = mqtt_client
self.topic = topic 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 # initialisations
dict.__init__(self) dict.__init__(self)
mqtt_client.add_callback( mqtt_client.add_callback(
@ -94,17 +96,17 @@ class base(dict):
self.__previous__[key] = prev_value self.__previous__[key] = prev_value
# Filter, if needed # Filter, if needed
self.unpack_filter(key) 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)) self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key))
elif key not in self.RX_IGNORE_KEYS: 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: else:
logger.debug("Ignoring key %s", key) self.logger.debug("Ignoring key %s", key)
def unpack(self, message): def unpack(self, message):
content_key = message.topic[len(self.topic) + 1:] 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): 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): if is_json(message.payload):
data = json.loads(message.payload) data = json.loads(message.payload)
if type(data) is dict: if type(data) is dict:
@ -118,7 +120,7 @@ class base(dict):
content_key, message.payload.decode('utf-8')) content_key, message.payload.decode('utf-8'))
self.warning_caller() self.warning_caller()
else: else:
logger.debug("Ignoring topic %s", content_key) self.logger.debug("Ignoring topic %s", content_key)
def pack_filter(self, key, data): def pack_filter(self, key, data):
if key in self.TX_FILTER_DATA_KEYS: if key in self.TX_FILTER_DATA_KEYS:
@ -137,10 +139,9 @@ class base(dict):
data = self.pack_filter(key, data) data = self.pack_filter(key, data)
if self.TX_TOPIC is not None: if self.TX_TOPIC is not None:
if self.TX_TYPE < 0: if self.TX_TYPE < 0:
logger.error( self.logger.error("Unknown tx type. Set TX_TYPE of class to a known value")
"Unknown tx type. Set TX_TYPE of class to a known value")
else: 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: if self.TX_TYPE == self.TX_DICT:
self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data})) self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
else: else:
@ -148,8 +149,7 @@ class base(dict):
data = json.dumps(data) 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) self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
else: else:
logger.error( self.logger.error("Unknown tx toptic. Set TX_TOPIC of class to a known value")
"Unknown tx toptic. Set TX_TOPIC of class to a known value")
def add_callback(self, key, data, callback, on_change_only=False): def add_callback(self, key, data, callback, on_change_only=False):
""" """
@ -175,7 +175,7 @@ class base(dict):
def warning_caller(self): def warning_caller(self):
if self.warning_call_condition(): if self.warning_call_condition():
warn_txt = self.warning_text() warn_txt = self.warning_text()
logger.warning(warn_txt) self.logger.warning(warn_txt)
if self.warning_callback is not None: if self.warning_callback is not None:
self.warning_callback(self, warn_txt) self.warning_callback(self, warn_txt)
@ -274,11 +274,11 @@ class shelly(base):
self.pack(self.KEY_OUTPUT_0, state) self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data): 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) self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, 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') self.set_output_0('toggle')
def set_output_1(self, state): def set_output_1(self, state):
@ -286,11 +286,11 @@ class shelly(base):
self.pack(self.KEY_OUTPUT_1, state) self.pack(self.KEY_OUTPUT_1, state)
def set_output_1_mcb(self, device, key, data): 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) self.set_output_1(data)
def toggle_output_1_mcb(self, device, key, 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') self.set_output_1('toggle')
@ -328,11 +328,11 @@ class silvercrest_powerplug(base):
self.pack(self.KEY_OUTPUT_0, state) self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data): 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) self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, 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') self.set_output_0('toggle')
@ -417,11 +417,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_0, state) self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data): 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) self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, 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') self.set_output_0('toggle')
def set_output_1(self, state): def set_output_1(self, state):
@ -429,11 +429,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_1, state) self.pack(self.KEY_OUTPUT_1, state)
def set_output_1_mcb(self, device, key, data): 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) self.set_output_1(data)
def toggle_output_1_mcb(self, device, key, 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') self.set_output_1('toggle')
def set_output_2(self, state): def set_output_2(self, state):
@ -441,11 +441,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_2, state) self.pack(self.KEY_OUTPUT_2, state)
def set_output_2_mcb(self, device, key, data): 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) self.set_output_2(data)
def toggle_output_2_mcb(self, device, key, 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') self.set_output_2('toggle')
def set_output_3(self, state): def set_output_3(self, state):
@ -453,11 +453,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_3, state) self.pack(self.KEY_OUTPUT_3, state)
def set_output_3_mcb(self, device, key, data): 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) self.set_output_3(data)
def toggle_output_3_mcb(self, device, key, 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') self.set_output_3('toggle')
def set_output_all(self, state): def set_output_all(self, state):
@ -465,11 +465,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_ALL, state) self.pack(self.KEY_OUTPUT_ALL, state)
def set_output_all_mcb(self, device, key, data): 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) self.set_output_all(data)
def toggle_output_all_mcb(self, device, key, 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') self.set_output_0('toggle')
@ -537,11 +537,11 @@ class tradfri_light(base):
self.pack(self.KEY_OUTPUT_0, state) self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data): 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) self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, 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') self.set_output_0('toggle')
def set_brightness(self, brightness): def set_brightness(self, brightness):
@ -549,7 +549,7 @@ class tradfri_light(base):
self.pack(self.KEY_BRIGHTNESS, brightness) self.pack(self.KEY_BRIGHTNESS, brightness)
def set_brightness_mcb(self, device, key, data): 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) self.set_brightness(data)
def default_inc(self, speed=40): def default_inc(self, speed=40):
@ -566,7 +566,7 @@ class tradfri_light(base):
self.pack(self.KEY_COLOR_TEMP, color_temp) self.pack(self.KEY_COLOR_TEMP, color_temp)
def set_color_temp_mcb(self, device, key, data): 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) self.set_color_temp(data)
@ -696,7 +696,7 @@ class nodered_gui_switch(nodered_gui_button):
self.pack(self.KEY_STATE, data) self.pack(self.KEY_STATE, data)
def set_state_mcb(self, device, key, 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) self.set_state(data)
@ -735,7 +735,7 @@ class nodered_gui_brightness_color_temp(base):
self.pack(self.KEY_ENABLE, data) self.pack(self.KEY_ENABLE, data)
def set_enable_mcb(self, device, key, 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) self.set_enable(data)
def set_brightness(self, data): def set_brightness(self, data):
@ -743,7 +743,7 @@ class nodered_gui_brightness_color_temp(base):
self.pack(self.KEY_BRIGHTNESS, data) self.pack(self.KEY_BRIGHTNESS, data)
def set_brightness_mcb(self, device, key, 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) self.set_brightness(data)
def set_color_temp(self, 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) self.pack(self.KEY_COLOR_TEMP, data)
def set_color_temp_mcb(self, device, key, 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) self.set_color_temp(data)
@ -773,7 +773,7 @@ class nodered_gui_leds(base):
def set_led(self, key, data): def set_led(self, key, data):
"""data: [True, False]""" """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) self.pack(key, data)
@ -828,7 +828,7 @@ class brennenstuhl_heatingvalve(base):
self.pack(self.KEY_HEATING_SETPOINT, setpoint) self.pack(self.KEY_HEATING_SETPOINT, setpoint)
def set_heating_setpoint_mcb(self, device, key, data): 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) self.set_heating_setpoint(data)
@ -891,7 +891,7 @@ class status(base):
self.pack(self.KEY_STATE + "/" + str(num), data) self.pack(self.KEY_STATE + "/" + str(num), data)
def set_state_mcb(self, device, key, 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) self.set_state(data)

2
mqtt

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