This repository contains example implementations for publishing logs to the uLogger service. uLogger provides a centralized logging platform that accepts log messages via MQTT and processes them for analysis and monitoring.
The python-log-publish directory contains a complete Python implementation for publishing logs to uLogger via MQTT.
The Python example demonstrates how to:
- Connect to the uLogger MQTT broker (
mqtt.ulogger.ai:8883) - Format log messages as NDJSON (newline-delimited JSON)
- Handle message batching with proper size limits
- Implement sequence numbering for deduplication
- Manage TLS certificate-based authentication
mqtt_example.py- Main example implementation with configurable customer/application IDsrequirements.txt- Python dependenciescertificate.pem.crt- TLS client certificate (required for authentication)private.pem.key- TLS private key (required for authentication)
Log messages are published as NDJSON (newline-delimited JSON) with the following structure:
{"sequence": 12374, "device_type": "DoorSenseV2", "version": "4.24.2", "git": "bc53443", "serial": "a87de76"}{"t": 0, "lv": 2, "m": "boot ok"}
{"t": 15, "lv": 3, "m": "temp=28.3"}
{"t": 1020, "lv": 4, "m": "warn: radio retry"}
{"t": 1150, "lv": 5, "m": "err: i2c"}sequence: Monotonically increasing sequence number for deduplicationdevice_type: Type of device generating logsversion: Software versiongit: Git commit hashserial: Device serial number
t: Timestamp (Unix timestamp in seconds)lv: Log level (1=DEBUG, 2=INFO, 3=WARNING, 4=ERROR, 5=CRITICAL)m: Log message content
- Broker:
mqtt.ulogger.ai - Port:
8883(TLS/SSL) - Topic Format:
/logs/v0/{customer_id}/{application_id} - Authentication: TLS client certificates
- Maximum batch size: 128KB
- Batching behavior: Messages exceeding 128KB are automatically split into multiple batches
- Error handling: Messages over 128KB are rejected with
CLIENT_ERROR: PAYLOAD_LIMIT_EXCEEDED
Each batch always begins with a header line containing the sequence number, followed by one or more log message lines.
Sequence numbers serve as deduplication identifiers:
- Must be monotonically increasing within an application session
- Should start with a random number at application startup
- Does not need to be globally unique across all time
- Should not repeat in the near future to ensure proper deduplication
-
Install Dependencies
pip install -r requirements.txt
-
Certificate Setup
- Place your TLS client certificate in
certificate.pem.crt - Place your TLS private key in
private.pem.key - Ensure both files are in the same directory as the Python script
- Place your TLS client certificate in
-
Configure Customer and Application IDs
Edit
mqtt_example.pyand update the following variables:CUSTOMER_ID = 123456789 # Replace with your actual customer ID APPLICATION_ID = 123456789 # Replace with your actual application ID
-
Run the Example
python mqtt_example.py
from mqtt_example import ULoggerClient, LogLevel
# Initialize client
client = ULoggerClient(customer_id=12345)
client.connect()
# Define header information
header = {
"git": "bc53443",
"serial": "a87de76",
"version": "4.24.2",
"device_type": "DoorSenseV2"
}
# Create log batch
logs = [
{"msg": "boot ok", "level": LogLevel.INFO},
{"msg": "temp=28.3", "level": LogLevel.INFO},
{"msg": "warn: radio retry", "level": LogLevel.WARNING},
{"msg": "err: i2c", "level": LogLevel.ERROR}
]
# Publish logs
client.publish_logs(header, logs, application_id=67890)
# Clean up
client.disconnect()The client includes error handling for common scenarios:
- Missing certificate files
- Connection failures
- Publishing failures
- Automatic batch splitting for oversized payloads
The implementation supports standard log levels:
LogLevel.DEBUG(1)LogLevel.INFO(2)LogLevel.WARNING(3)LogLevel.ERROR(4)LogLevel.CRITICAL(5)
- TLS certificates should be kept secure and not committed to version control
- Use appropriate file permissions for certificate and key files
- Consider certificate rotation policies for production deployments
- Certificate Issues: Ensure certificate and key files exist and have correct permissions
- Connection Issues: Verify network connectivity to
mqtt.ulogger.ai:8883 - Publishing Failures: Check that customer and application IDs are correctly configured
- Size Limit Errors: The client automatically handles batch splitting, but individual log messages should not exceed 128KB
paho-mqtt>=1.6.1- MQTT client library for Python
The example_upload directory demonstrates how to integrate uLogger firmware upload automation into your CI/CD pipeline using GitHub Actions.
This example shows how to:
- Automatically build and upload firmware to uLogger during CI/CD
- Extract and track version information from git tags and commits
- Securely manage MQTT certificates and authentication credentials
- Integrate with existing GitHub Actions workflows
- Monitor and verify firmware uploads
- Automated Builds: Trigger firmware builds on push, pull requests, or manual dispatch
- Version Management: Automatic semantic versioning from git tags with fallback to development versions
- GitHub Secrets: Secure credential management for certificates and API keys
- Artifact Storage: Automatic backup of built firmware files
- Flexible Configuration: Easy integration with any build system (Make, CMake, custom scripts, etc.)
The workflow automatically:
- Checks out your repository
- Builds your firmware (or uses the provided sample)
- Extracts version from git tags or generates a development version
- Uploads the firmware to uLogger with metadata (git hash, branch, version)
- Stores the built firmware as a GitHub artifact
- Fork or clone the uLogger example_upload repository
- Configure GitHub Secrets with your uLogger credentials:
ULOGGER_CUSTOMER_IDULOGGER_APPLICATION_IDULOGGER_DEVICE_TYPEULOGGER_CERT_DATA(MQTT certificate)ULOGGER_KEY_DATA(MQTT private key)
- Customize
.github/workflows/build-and-upload.ymlwith your build commands - Push changes and the workflow will automatically upload to uLogger
For complete documentation and working examples, visit: ulogger-ai/example_upload
For more information about uLogger or additional examples, please refer to the official documentation.