Skip to main content

A Python logging handler that bridges Python's logging module with Telegram, enabling direct sending of log messages and files to Telegram chats.

Project description

Teletracker

PyPI version License

A bridge between Python's logging module and Telegram: send log messages and files directly to your chat.

📦 Installation

Install the package from PyPI with:

pip install teletracker

🚀 Overview

The library provides a handler for Python's logging framework and a dedicated class for sending files via Telegram.

Main Classes

UnifiedTgLogger

Inherits from logging.Handler; sends formatted log messages via Telegram.

Constructor:

UnifiedTgLogger(
    token: str,
    users: List[int],
    timeout: int = 10,
    level: int = logging.INFO,
    log_emoji: Optional[str] = None,
    use_random_log_emoji: bool = False,
    disable_notification: bool = False,
    disable_web_page_preview: bool = False
)

Parameters:

  • token – Telegram bot token.
  • users – List of chat_id to send logs to.
  • timeout – HTTP timeout in seconds.
  • level – Log level (default INFO).
  • log_emoji – Fixed emoji for all messages.
  • use_random_log_emoji – If True, chooses a random emoji.
  • disable_notification – Send silently.
  • disable_web_page_preview – Disable link previews.

Methods:

  • get_emoji(levelno: int) -> str Returns the emoji based on the log level or settings.

  • emit(record: logging.LogRecord) Formats the record with HTML and sends the message to the bot. If exc_info is present, includes the traceback in a <pre><code> block.

  • send_file(file_path: str, caption: str = '') Sends a file (document or image); delegates to TgFileLogger.

  • setLevel(level: int) Dynamically changes the log level of the handler.

  • update_users(users: List[int]) Updates the list of users (chat_id) for messages and files.

TgFileLogger

Dedicated class for sending files via Telegram. Can be used directly or through UnifiedTgLogger.

Constructor:

TgFileLogger(
    token: str,
    users: List[int],
    timeout: int = 10
)

Main method:

  • send(file_path: str, caption: str = '') Sends a document or image with an optional caption.

📖 Usage Examples

Simple Logging

import logging
from teletracker.unified_logger import UnifiedTgLogger

logger = logging.getLogger('app')
logger.setLevel(logging.DEBUG)

handler = UnifiedTgLogger(
    token='YOUR_BOT_TOKEN_HERE',  # Replace with your bot token
    users=[YOUR_CHAT_ID_HERE]     # Replace with your chat ID
)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info('Application started')
logger.error('An error was recovered')

try:
    1/0
except Exception:
    logger.exception('Division by zero occurred')

Sending Files

from teletracker.unified_logger import UnifiedTgLogger

# Assuming 'token' and 'users' are defined as in the simple logging example
# token = 'YOUR_BOT_TOKEN_HERE'
# users = [YOUR_CHAT_ID_HERE]

file_logger = UnifiedTgLogger(token, users) # Or TgFileLogger(token, users)
file_logger.send_file(
    'report.txt',
    caption='Daily report'
)

🙏 Acknowledgements

This project was originally forked from otter18/tg_logger. The original library provided a great starting point. This fork was created because the original project was no longer actively maintained and lacked certain features. While the codebase has been significantly refactored and rewritten to add new functionalities and improvements, we acknowledge the foundational work of the original author.

📜 License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

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

teletracker-0.0.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

teletracker-0.0.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file teletracker-0.0.1.tar.gz.

File metadata

  • Download URL: teletracker-0.0.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for teletracker-0.0.1.tar.gz
Algorithm Hash digest
SHA256 030071b335fbab37e8321aebf59e20c30e4d04737e25bc35acfc77544e808ca1
MD5 0b23ad9710a71e823a66eb514cc677b2
BLAKE2b-256 6f022d995ab2ce61ac4ee54239885189717aa1d7bd67942a66a587b79efa64b2

See more details on using hashes here.

File details

Details for the file teletracker-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: teletracker-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for teletracker-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f81f0d8438fcded5f5b001613533a22839411b493a73c80085ba205e9fc7fe5a
MD5 9909cb1ec3690f8491e8f69b8be70eb2
BLAKE2b-256 250114323ed13e82c8936af84cf0e94c427c736529ad58b513bb04158866c65c

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