Skip to main content

A logger that logs messages to an MQTT topic with systemd journal integration

Project description

MQTT Logger

MQTT Logger is a Python package designed for logging messages to an MQTT topic. It provides an easy-to-use interface for connecting to an MQTT broker and logging messages at various levels (debug, info, warning, error, critical). The logger supports batching of log messages and can be used both as a standalone logger and as an asynchronous context manager.

Features

  • Log messages to an MQTT topic.
  • Support for multiple logging levels.
  • Automatic batching of log messages.
  • Asynchronous operation with support for context management.
  • Metrics tracking for logging performance.
  • Systemd journal integration for structured logging.
  • Optional file-based logging.

Requirements

  • Python 3.7+
  • systemd development libraries (for systemd journal integration)
  • MQTT broker access

Installation

System Dependencies

On Ubuntu/Debian systems, install the required systemd development libraries:

sudo apt-get install libsystemd-dev pkg-config

On CentOS/RHEL/Fedora systems:

sudo yum install systemd-devel pkgconfig
# or on newer versions:
sudo dnf install systemd-devel pkgconfig

Python Package

To install the MQTT Logger package, you can use pip:

pip install .

Note: This package depends on the muxu-io-mqtt-connector PyPI package and requires systemd to be available.

Make sure to run this command in the root directory of the project where the setup.py file is located.

Usage

Basic Usage

You can use the MqttLogger class to log messages as follows:

from mqtt_logger.logger import MqttLogger

async def main():
    logger = MqttLogger(
        mqtt_broker="mqtt.example.com",
        mqtt_port=1883,
        mqtt_topic="logs/myapp",
        log_file="app.log"
    )
    await logger.connect_mqtt()
    logger.info("Application started")
    await logger.shutdown()

Context Manager Usage

The MqttLogger can also be used as an asynchronous context manager:

from mqtt_logger.logger import MqttLogger

async def main():
    async with MqttLogger(
        mqtt_broker="mqtt.example.com",
        mqtt_port=1883,
        mqtt_topic="logs/myapp"
    ) as logger:
        logger.info("In context manager")

Running Tests

To run the tests for the MQTT Logger, you can use the following command:

python3 -m venv .venv
source .venv/bin/activate && pip install -r requirements.txt
pytest tests/

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

muxu_io_mqtt_logger-1.2.9.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

muxu_io_mqtt_logger-1.2.9-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file muxu_io_mqtt_logger-1.2.9.tar.gz.

File metadata

  • Download URL: muxu_io_mqtt_logger-1.2.9.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for muxu_io_mqtt_logger-1.2.9.tar.gz
Algorithm Hash digest
SHA256 894078c66f1ca494753a80d03ccff701767f0d5d628fb52106eca6c2f5634f33
MD5 beaaa299d509ee79f7661b7688096cc5
BLAKE2b-256 806bdd26f90516b7ba484afd03a1604dd94796e356b9d00dbd13d5588f0ef84e

See more details on using hashes here.

Provenance

The following attestation bundles were made for muxu_io_mqtt_logger-1.2.9.tar.gz:

Publisher: publish.yml on muxu-io/mqtt-logger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file muxu_io_mqtt_logger-1.2.9-py3-none-any.whl.

File metadata

File hashes

Hashes for muxu_io_mqtt_logger-1.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 edf61be69c789aff3196e4567534b6183c7fc354857a52e613ef91967348b2d3
MD5 a42aef707106daea29eb343d2d527a57
BLAKE2b-256 3fd09bbda7e5233d9a8470ced53b6f2a03cdff0108a241fbb9570b947bf966ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for muxu_io_mqtt_logger-1.2.9-py3-none-any.whl:

Publisher: publish.yml on muxu-io/mqtt-logger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page