Documentation / LemmeKnow Server

Logs

A log is a single line of text your script sends to LemmeKnow. Each line has a level, a source, and a message. On your phone it appears in the Monitor tab, the Server Monitor widget, and anywhere else server logs show up, and your rules decide whether it notifies you.

Scripts send logs with the helper module log_client.py, which comes with LemmeKnow Server and works the same with the LemmeSee Hub.

Quick start

import log_client

log_client.SOURCE = "backup"

log_client.info("Backup started")
log_client.warning("Disk at 85%")
log_client.error("Backup failed: target not reachable")

Each call publishes one log line and waits (up to 5 seconds) for the broker to confirm it, so the last message of a short script isn't lost when the script exits. Every log message here is identified in LemmeKnow as belonging to the source identifier "backup" which it can be filtered on.

Where to run your scripts

The helper needs two things from the server's folder: config.py, for the broker address, and the server's database, for the server id that goes into every topic. The easiest arrangement is to keep your script in the same folder as lemmeknow.py, log_client.py and config.py, on the same PC, and to start the server at least once before your first script runs.

To use the helper on another machine, copy log_client.py there, set the broker address, and give it your server id, either in your script or with the LEMMEKNOW_SERVER_ID environment variable:

import log_client
log_client.MQTT_BROKER = "192.168.1.10"
log_client.SERVER_ID = "a1b2c3d4"

The broker must accept anonymous connections from that machine, because the helper doesn't sign in. See Finding your server id and Setting up Mosquitto.

Levels

Levels, from least to most severe:

LevelFunctionDefault color
debugdebug()Grey
infoinfo()White
warningwarning() (also warn())Yellow
importantimportant()Cyan
errorerror() (also err())Red

Colors are the app's defaults and can be changed in its appearance settings. Whether a level notifies you, and how, is up to the rules you set up in the app. See Rules and Filters.

Sources

The source is the name a log line is filed under, shown as [sourceName] in the Monitor tab. The app remembers the sources it has seen on each server.

Set the default source once at the top of your script with log_client.SOURCE. If you don't, it is the PC's hostname. You can override it on any single call with source=.

Pick short names without spaces or slashes, such as backup, build, or nas. The source also becomes the last part of the MQTT topic; there, the characters /, +, #, and whitespace are stripped out, and an empty result becomes unknown. The message itself keeps the source exactly as you wrote it.

Public and private

Every log line is either public or private. Private is the default. Pass public=True to send to the public channel. See Public and private channels for who receives each.

log_client.info("Nightly backup finished", public=True)
log_client.debug("Copied 1,204 files")

To send the entries of log files your programs already write, without changing them, see Autologger.

Functions

All of these are module-level functions in log_client.

FunctionArgumentsWhat it does
debugmessage, source=None, public=False, media_url=NoneSends a debug line.
infomessage, source=None, public=False, media_url=NoneSends an info line.
warningmessage, source=None, public=False, media_url=NoneSends a warning line. warn is the same function.
importantmessage, source=None, public=False, media_url=NoneSends an important line.
errormessage, source=None, public=False, media_url=NoneSends an error line. err is the same function.
print_debugmsg, source=NonePrints the message to the console, then sends it as a private debug line.
print_infomsg, source=NoneSame, at info.
print_warningmsg, source=NoneSame, at warning.
print_importantmsg, source=NoneSame, at important.
print_errormsg, source=NoneSame, at error.
pingcode, message="", source=None, public=False, user_topic_id=NoneSends a ping. See Pings. user_topic_id is hub only; see Messages for one user.
statename, value, label=None, display="text", group=None, group_label=None, alarm=False, interval=0, fields=None, sort_order=None, hide_label=FalseSets a state. Shown only when this server is relayed through a LemmeSee Hub; see States from a relayed server.
clear_statename, group=NoneRemoves a state set with state.

source=None means "use log_client.SOURCE". media_url is the address of an image to show with the line; each server's settings in the app control whether images are shown inline. The print_ functions always send privately. In their console copy, Markdown-style links such as [report](https://example.com/report) are converted to a text hyperlink.

Module settings

NameDefaultMeaning
SOURCEThe PC's hostnameDefault source for every call.
MQTT_BROKERFrom config.py, else localhostBroker address.
MQTT_PORTFrom config.py, else 1883Broker port.
SERVER_IDThe LEMMEKNOW_SERVER_ID environment variable, else emptyServer id to publish under. When empty, the helper reads it from the server's database.

The helper connects without a username or password. Point it at a broker listener that allows anonymous access, such as the local listener described in Setting up Mosquitto.

The Logger class

If you prefer an object with a fixed source, use Logger:

from log_client import Logger

log = Logger("build")
log.info("Build started")
log.error("Tests failed", public=True)
log.ping("200", "Build finished")
MethodArguments
debug, info, warning, important, errormessage, public=False, media_url=None
warn, errSame as warning and error.
pingcode, message="", public=True
state, clear_stateSame as the module functions, with the logger's source as the group.

Note that Logger.ping is public by default, while the module-level ping() is private by default.

When the broker can't be reached

The helper never raises an error into your script. If it can't connect within about 3 seconds, or the broker doesn't confirm a message, it prints a line such as [log_client] publish failed: ... and your script carries on. It tries to connect again on the next call.

Sending without the helper

Any MQTT client can send a log. Publish a JSON message, not retained, to:

lemmeknow/<server_id>/logs/public/<source>
lemmeknow/<server_id>/logs/private/<source>
FieldRequiredMeaning
levelNoOne of the five levels. The app treats a missing level as debug.
sourceNoSource name. The app treats a missing source as unknown.
msgYesThe message text.
tsNoTime in milliseconds (Unix epoch). The app uses the arrival time if missing.
media_urlNoURL of an image to show with the line. The helper sets it from its media_url argument. Each server's settings in the app control whether images are shown inline.
mosquitto_pub -h 192.168.1.10 -p 1883 -q 1 \
  -t 'lemmeknow/a1b2c3d4/logs/public/doorbell' \
  -m '{"level":"important","source":"doorbell","msg":"Someone is at the door","media_url":"https://example.com/snapshot.jpg"}'

Replace a1b2c3d4 with your server id. See Finding your server id.