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

Uploaded Python 3

File details

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

File metadata

  • Download URL: muxu_io_mqtt_logger-1.2.8.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.8.tar.gz
Algorithm Hash digest
SHA256 3f862cece6d320c897b60de4d0e26ea5a0f552a572c1490c610933d81307f904
MD5 47f289944ec914621cff93762fe10337
BLAKE2b-256 acf9945dc70c46e41890a694e11b832921ca189c35b06dd2a88c5dc9473fed8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for muxu_io_mqtt_logger-1.2.8.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.8-py3-none-any.whl.

File metadata

File hashes

Hashes for muxu_io_mqtt_logger-1.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 78e58bf2502a0fb9cdff1019bf4c57380e0306d5977f9a9865f2a9562c7ac0aa
MD5 7b4c410d3b17cfcd736d9eee8896c56f
BLAKE2b-256 6758bb03cea64287ab741605a2492b308c6cf298c3b7cd9dabfdc236a529a166

See more details on using hashes here.

Provenance

The following attestation bundles were made for muxu_io_mqtt_logger-1.2.8-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