Messenger Logger Callback — send training logs to Telegram via a remote server. Standalone logger and Hugging Face Trainer callback.
Project description
messenger-logger-callback
messenger-logger-callback (messenger_logger_callback) — a Python library for sending training logs to a remote server with real-time Telegram notifications. Works as a standalone logger for any training loop (PyTorch, Lightning, custom) or as a Hugging Face Trainer Callback.
Pairs with telegram-log-service for real-time Telegram bot alerts, metric monitoring, and stalled run detection.
Installation
pip install messenger-logger-callback
Or equivalently:
pip install messenger_logger_callback
This installs everything including Hugging Face Transformers Trainer support. If you only need the standalone logger and want to avoid the transformers dependency, install with --no-deps and add requests and python-dotenv manually.
Quick Start: Standalone Logger
Use MessengerLogger in any training loop — plain PyTorch, Lightning, or anything else.
from messenger_logger import MessengerLogger
logger = MessengerLogger(
server_url="http://your-server:5000/api/logs",
project_name="resnet_experiment",
run_id="run_v3",
author_username="riko",
)
logger.start()
for epoch in range(num_epochs):
for step, batch in enumerate(dataloader):
loss = train_step(batch)
global_step += 1
logger.log(step=global_step, epoch=epoch, metrics={"loss": loss.item()})
logger.epoch_end(epoch)
logger.finish()
Available Methods
| Method | Description |
|---|---|
start() |
Signal training start. Begins heartbeat. |
log(step, metrics, epoch=None) |
Log training metrics at a given step. |
epoch_end(epoch) |
Signal end of an epoch. |
log_custom(data) |
Send arbitrary custom data. |
finish() |
Signal training end. Stops heartbeat. |
Quick Start: Hugging Face Trainer
Use MessengerLoggerCallback as a drop-in Trainer callback.
from transformers import Trainer, TrainingArguments
from messenger_logger.callback import MessengerLoggerCallback
logger = MessengerLoggerCallback(
server_url="http://your-server:5000/api/logs",
project_name="bert_finetune",
run_id="experiment_v2",
auth_token="your_secret_token",
author_username="riko",
metadata={"model": "bert-large", "dataset": "squad"},
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
callbacks=[logger],
)
trainer.train()
The callback automatically captures on_log, on_train_begin, on_train_end, and on_epoch_end events.
You can also send custom data at any point:
logger.send_custom_log({
"event": "evaluation_complete",
"accuracy": 0.95,
"f1": 0.93,
})
Heartbeat
A background thread sends a lightweight heartbeat signal to the server every 60 seconds by default. This allows the server to detect crashed or stalled runs much faster than waiting for missing log events.
- On by default with a 60-second interval.
- Starts when training begins, stops when training ends.
- If the training process crashes, the heartbeat stops automatically.
To change the interval or disable:
# Custom interval (30 seconds)
logger = MessengerLogger(server_url="...", heartbeat_interval=30)
# Disable heartbeat
logger = MessengerLogger(server_url="...", heartbeat_interval=None)
ClearML Integration
If ClearML is active in your training script, the library automatically detects the current task and includes a link to the ClearML dashboard in every payload sent to the server. No configuration needed — if clearml is installed and a task is running, the link is captured.
Configuration
Settings can be provided via constructor arguments, environment variables, or a .env file. Constructor arguments take highest precedence, then environment variables, then .env file values.
| Constructor Argument | Environment Variable | Description |
|---|---|---|
server_url |
MESSENGER_LOGGER_SERVER_URL |
HTTP endpoint to send logs to. Required. |
project_name |
— | Project identifier. Default: "default_project". |
run_id |
— | Unique run identifier. Auto-generated if omitted. |
auth_token |
MESSENGER_LOGGER_AUTH_TOKEN |
Bearer token for the Authorization header. |
author_username |
MESSENGER_LOGGER_AUTHOR_USERNAME |
Who started the run. Default: "anonymous". |
metadata |
MESSENGER_LOGGER_METADATA |
Static metadata dict. Env var should be a JSON string. |
dotenv_path |
MESSENGER_LOGGER_DOTENV |
Path to a .env file to load config from. |
heartbeat_interval |
— | Seconds between heartbeats. Default: 60. Set to None to disable. |
Error Handling
Network errors (timeouts, connection failures, HTTP errors) are caught and printed as warnings. They never crash your training. Example messages:
Warning: Request to http://... timed out for step 10.
Error: Could not connect to server at http://... for step 20.
Error: HTTP error occurred while sending logs for step 30. Status: 401.
Related Projects
- telegram-log-service — the server that receives logs from this library and forwards them as Telegram bot alerts.
License
MIT
messenger-logger-callback · messenger_logger_callback · pip install messenger-logger-callback · pip install messenger_logger_callback · Telegram training logger · Hugging Face Trainer callback logger · ML training Telegram notifications
Project details
Release history Release notifications | RSS feed
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 messenger_logger_callback-0.3.3.tar.gz.
File metadata
- Download URL: messenger_logger_callback-0.3.3.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6fce6c80c0e890bd9c77d9867d59391c11e3bec428bb672de093dd1ce1993c9
|
|
| MD5 |
1f6eb1efc7707e9b4275f058a7e13b3b
|
|
| BLAKE2b-256 |
dc36ebffd3512f17a3259bd19826a5db1af7c8ec96ac96b97d158602d82a9d08
|
File details
Details for the file messenger_logger_callback-0.3.3-py3-none-any.whl.
File metadata
- Download URL: messenger_logger_callback-0.3.3-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c7f90d91a603e392abdac4dea734314b3042c5c20998430f665f894dab381dd
|
|
| MD5 |
19da6a286c64a464b3e5284a29c88477
|
|
| BLAKE2b-256 |
b5ffd294555356a47f813915d9e8a258a1351e5e7d1705d8634eff5231fbc179
|