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.3.0.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.3.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: muxu_io_mqtt_logger-1.3.0.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.3.0.tar.gz
Algorithm Hash digest
SHA256 8ceaf355a2f15bf07b7103523414410e088c47b72bbca65cd7ed76655b181a45
MD5 5a71db879bcf3f9be5f5e5898168e1ed
BLAKE2b-256 b15f0b5b5ecb55ec8c5a65221736972811fd553d6bb90b99b66440352e7c34c5

See more details on using hashes here.

Provenance

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

Publisher: pypi-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.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for muxu_io_mqtt_logger-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a75da250cbc967971dfd2d44c210efa71c8a0bfd1c68c82e9f47839dcdb54142
MD5 4c6fc4dc8c69c411bcc1c0ff0ecb3b23
BLAKE2b-256 8f919285cf4ec7b08df03216fc58cd5441c348a9e1c79febc53a6c81f953bee4

See more details on using hashes here.

Provenance

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

Publisher: pypi-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