Source code for juham.ts.log
import json
from typing import Any, override
from influxdb_client_3 import Point
from juham.base import Base, MqttMsg
from juham.base.time import epoc2utc
[docs]
class LogRecord(Base):
"""Class recording application events, such as warnigns and errors,
to time series database."""
def __init__(self, name: str = "log_record") -> None:
"""Creates mqtt client for recording log events to time series
database.
Args:
name (str): name for the client
"""
super().__init__(name)
self.topic_name = self.make_topic_name("log")
[docs]
@override
def on_connect(self, client: object, userdata: Any, flags: int, rc: int) -> None:
"""Connects the client to mqtt broker.
Args:
client (obj): client to be connected
userdata (any): caller specific data
flags (int): implementation specific shit
Returns:
rc (bool): True if succesful
"""
super().on_connect(client, userdata, flags, rc)
if rc == 0:
self.subscribe(self.topic_name)
[docs]
@override
def on_message(self, client: object, userdata: Any, msg: MqttMsg) -> None:
m = json.loads(msg.payload.decode())
ts = epoc2utc(m["Timestamp"])
point = (
Point("log")
.tag("class", m["Class"])
.field("source", m["Source"])
.field("msg", m["Msg"])
.field("details", m["Details"])
.field("Timestamp", m["Timestamp"])
.time(ts)
)
try:
self.write(point)
except Exception as e:
self.log_message("Error", f"Cannot write log event {m['Msg']}", str(e))