Skip to content

Commit 722fef9

Browse files
committed
make the handler a bit more resilient
1 parent 31cb610 commit 722fef9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

examples/mqtt_handler.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ def __init__(self, mqtt_client: MQTT.MQTT, topic: str) -> None:
3030

3131
def emit(self, record: LogRecord) -> None:
3232
"""
33-
Publish message from the LogRecord to the MQTT broker.
33+
Publish message from the LogRecord to the MQTT broker, if connected.
3434
"""
35-
self._mqtt_client.publish(self._topic, record.msg)
35+
try:
36+
if self._mqtt_client.is_connected():
37+
self._mqtt_client.publish(self._topic, record.msg)
38+
except MQTT.MMQTTException:
39+
pass
3640

3741
# To make this work also in CPython's logging.
3842
def handle(self, record: LogRecord) -> None:

0 commit comments

Comments
 (0)