A bridge between Python's logging module and Telegram: send log messages and files directly to your chat.
Install the package from PyPI with:
pip install teletrackerThe library provides a handler for Python's logging framework and a dedicated class for sending files via Telegram.
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 ofchat_idto send logs to.timeout– HTTP timeout in seconds.level– Log level (defaultINFO).log_emoji– Fixed emoji for all messages.use_random_log_emoji– IfTrue, chooses a random emoji.disable_notification– Send silently.disable_web_page_preview– Disable link previews.
Methods:
-
get_emoji(levelno: int) -> strReturns 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. Ifexc_infois present, includes the traceback in a<pre><code>block. -
send_file(file_path: str, caption: str = '')Sends a file (document or image); delegates toTgFileLogger. -
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.
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.
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')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'
)- password-protect the bot token
- too long messages are cut off
- use a daemon background thread, using Python's internal QueueHandler.
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.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.