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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
894078c66f1ca494753a80d03ccff701767f0d5d628fb52106eca6c2f5634f33
|
|
| MD5 |
beaaa299d509ee79f7661b7688096cc5
|
|
| BLAKE2b-256 |
806bdd26f90516b7ba484afd03a1604dd94796e356b9d00dbd13d5588f0ef84e
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
muxu_io_mqtt_logger-1.2.9.tar.gz -
Subject digest:
894078c66f1ca494753a80d03ccff701767f0d5d628fb52106eca6c2f5634f33 - Sigstore transparency entry: 403066597
- Sigstore integration time:
-
Permalink:
muxu-io/mqtt-logger@f571a91075b5cd6259660a116a239f67f106a7dd -
Branch / Tag:
refs/tags/v1.2.9 - Owner: https://github.com/muxu-io
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f571a91075b5cd6259660a116a239f67f106a7dd -
Trigger Event:
push
-
Statement type:
File details
Details for the file muxu_io_mqtt_logger-1.2.9-py3-none-any.whl.
File metadata
- Download URL: muxu_io_mqtt_logger-1.2.9-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edf61be69c789aff3196e4567534b6183c7fc354857a52e613ef91967348b2d3
|
|
| MD5 |
a42aef707106daea29eb343d2d527a57
|
|
| BLAKE2b-256 |
3fd09bbda7e5233d9a8470ced53b6f2a03cdff0108a241fbb9570b947bf966ad
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
muxu_io_mqtt_logger-1.2.9-py3-none-any.whl -
Subject digest:
edf61be69c789aff3196e4567534b6183c7fc354857a52e613ef91967348b2d3 - Sigstore transparency entry: 403066614
- Sigstore integration time:
-
Permalink:
muxu-io/mqtt-logger@f571a91075b5cd6259660a116a239f67f106a7dd -
Branch / Tag:
refs/tags/v1.2.9 - Owner: https://github.com/muxu-io
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f571a91075b5cd6259660a116a239f67f106a7dd -
Trigger Event:
push
-
Statement type: