Skip to main content

messenger-logger-callback

PyPI version Python License: MIT

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

Metadata

Release files for messenger-logger-callback 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for messenger-logger-callback 0.3.3
File Size Uploaded
messenger_logger_callback-0.3.3.tar.gz 14.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for messenger-logger-callback 0.3.3
File Interpreter ABI Platform
messenger_logger_callback-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 25.6 kB

Release files / messenger_logger_callback-0.3.3.tar.gz

Download URL messenger_logger_callback-0.3.3.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b6fce6c80c0e890bd9c77d9867d59391c11e3bec428bb672de093dd1ce1993c9
BLAKE2b-256 checksum
How to use checksums
dc36ebffd3512f17a3259bd19826a5db1af7c8ec96ac96b97d158602d82a9d08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / messenger_logger_callback-0.3.3-py3-none-any.whl

Download URL messenger_logger_callback-0.3.3-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c7f90d91a603e392abdac4dea734314b3042c5c20998430f665f894dab381dd
BLAKE2b-256 checksum
How to use checksums
b5ffd294555356a47f813915d9e8a258a1351e5e7d1705d8634eff5231fbc179
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page