Skip to main content

A Python logging handler that sends log messages to Telegram chats

Project description

TGBot-Logging

PyPI version Python Support Documentation Status License: MIT Code Coverage Code Style: Black

A Python logging handler that sends log messages to Telegram chats with advanced features like message batching, retries, and formatting.

Features

  • Send log messages to one or multiple Telegram chats
  • Support for HTML and MarkdownV2 formatting
  • Message batching for better performance
  • Automatic retries for failed messages
  • Rate limiting and error handling
  • Customizable log format and emojis
  • Support for project names and hashtags
  • Environment variables support
  • Async/await support
  • Cross-platform compatibility
  • Type hints and documentation
  • 96% test coverage

Quick Start

  1. Install the package:
pip install tgbot-logging
  1. Basic usage:
import logging
from tgbot_logging import TelegramHandler

# Create logger
logger = logging.getLogger('MyApp')
logger.setLevel(logging.DEBUG)

# Create TelegramHandler
telegram_handler = TelegramHandler(
    token='YOUR_BOT_TOKEN',
    chat_ids=['YOUR_CHAT_ID'],
    level=logging.INFO,
    project_name='MyApp',  # Optional project name
    project_emoji='🚀',    # Optional project emoji
    parse_mode='HTML'      # Support for HTML formatting
)

# Add handler to logger
logger.addHandler(telegram_handler)

# Example usage
logger.info('This is an info message')
logger.error('This is an error message')
  1. Advanced usage with batching and retries:
telegram_handler = TelegramHandler(
    token='YOUR_BOT_TOKEN',
    chat_ids=['YOUR_CHAT_ID'],
    level=logging.INFO,
    batch_size=5,           # Batch 5 messages together
    batch_interval=2.0,     # Send batch every 2 seconds or when full
    max_retries=3,          # Retry failed messages 3 times
    retry_delay=1.0,        # Wait 1 second between retries
    parse_mode='HTML',      # Support for HTML formatting
    fmt='<b>%(levelname)s</b> [%(asctime)s]\n%(message)s'  # Custom HTML format
)
  1. Environment variables support:
# .env file
TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_CHAT_IDS=123456789,987654321
LOG_LEVEL=INFO
BATCH_SIZE=5
BATCH_INTERVAL=2.0
MAX_RETRIES=3
RETRY_DELAY=1.0
PARSE_MODE=HTML
PROJECT_NAME=MyProject
PROJECT_EMOJI=🚀

Documentation

Full documentation is available at tgbot-logging.readthedocs.io, including:

  • Detailed installation instructions
  • Configuration options
  • Advanced usage examples
  • API reference
  • Development guide

Features in Detail

Message Formatting

  • Support for HTML and MarkdownV2 formatting
  • Custom message formats with templates
  • Custom date/time formats
  • Project names and emojis
  • Automatic hashtags
  • Level-specific emojis

Message Batching

  • Configurable batch size
  • Configurable batch interval
  • Automatic batch flushing
  • Memory-efficient queue system

Error Handling

  • Automatic retries for failed messages
  • Rate limit handling
  • Network error handling
  • Timeout handling
  • Graceful error recovery

Performance

  • Asynchronous message sending
  • Message batching
  • Rate limiting
  • Memory optimization
  • Cross-platform compatibility

Development Features

  • Type hints for better IDE support
  • Comprehensive test suite (96% coverage)
  • Detailed documentation
  • Code style compliance (Black)
  • Security checks (Bandit)

Development Installation

For development with testing tools and code formatting:

pip install -e ".[dev]"
# or
pip install -r requirements-dev.txt

Testing

Run tests with coverage report:

pytest -v --cov=tgbot_logging --cov-report=term-missing:skip-covered

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests and linting
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

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

tgbot_logging-1.0.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

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

tgbot_logging-1.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file tgbot_logging-1.0.1.tar.gz.

File metadata

  • Download URL: tgbot_logging-1.0.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for tgbot_logging-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bf955902c492d00a690f9bb51c9bf1037318c9e377e71e198d9ae3dfa66e4709
MD5 356f8351803826510d0c176d67096bdd
BLAKE2b-256 ce40a91deddf95031a00024e7427248f6bb4a12255c52d9800e9b19b4147a5df

See more details on using hashes here.

File details

Details for the file tgbot_logging-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tgbot_logging-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for tgbot_logging-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad3a6d3edaca97b51fb450b28dbc3b6473bb757db1253ea70aef3886b736aef6
MD5 292e864fb36af682eef677673f50159d
BLAKE2b-256 e8dccc9a63a69e9e4735f740097f56cff020001d05eb70bd3cb8c97cd27163e9

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