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.0.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.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tgbot_logging-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 a336aff7673d75e277772d901baf7ee1e315e871cfefaa31861e6173a0997c1d
MD5 d3be7260c6cf5c2fd0d580870689f534
BLAKE2b-256 fcabd88e3fc8af7bb7672c8f44aff55d174febc1f86a5e2731d399cd94d9c827

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tgbot_logging-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ad00920a0e3e409b1c3996740752d007f7a7af33d7aea22e668449df44a87fa
MD5 59619b02ba46c907d84e435fdcc04832
BLAKE2b-256 d56960193cf0eec7495a0569f5ac1bcc5964100d7b4a92362d60716c853276f4

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