Skip to main content

Messenger Logger Callback — send training logs to Telegram via a remote server. Standalone logger and Hugging Face Trainer callback.

Project description

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

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

messenger_logger_callback-0.2.1.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

messenger_logger_callback-0.2.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file messenger_logger_callback-0.2.1.tar.gz.

File metadata

File hashes

Hashes for messenger_logger_callback-0.2.1.tar.gz
Algorithm Hash digest
SHA256 47b625e46315aa51ba2e7c2b3d306030dba6e095d132a095a7b03531ac8a7989
MD5 323e374ad9484bd6742be9a254f1f2ea
BLAKE2b-256 83e628b481dbb6b89709f633bc48187e131a8b410092d6498d6f1e731d47b7b2

See more details on using hashes here.

File details

Details for the file messenger_logger_callback-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for messenger_logger_callback-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f52a666197155467776dad0ababd5507f964b168b6f7606785b2b8c4d706414
MD5 87278916be491958921260f3811799da
BLAKE2b-256 de7f4579c5929a0cd1ad5254ee37c483fbe53d50eb62ea2d8fa1bca1e5571b06

See more details on using hashes here.

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