A python logging handler that sends logs via Telegram Bot Api.

sashgorokhov sashgorokhov Last update: Dec 07, 2023

python-telegram-handler

image

image

image

image

image

Code Climate

image

A python logging handler that sends logs via Telegram Bot Api

Installation

Via pip:

pip install python-telegram-handler

Usage

Register a new telegram bot and obtain a authentication token. (Instructions here https://core.telegram.org/bots#3-how-do-i-create-a-bot)

After that, you must obtain a chat_id. You can do that using included simple script. Start a new conversation with newly created bot, write something to it (it is important to initiate conversation first).

Also, there is an ability for handler to automatically retrieve chat_id. This will be done on handler initialization. But you still have to start a conversation with bot. Be aware: if program stops, server restarts, or something else will happen - handler will try to retrieve chat id from telegram, and may fail, if it will not find a NEW message from you. So i recommend you to use a script below for obtaining chat id.

Then run a command:

python -m telegram_handler <your token here>

If all went ok, a chat_id will be printed to stdout.

Using token and chat_id, configure log handler in your desired way. For example, using dictConfig:

{
    'version': 1,
    'handlers': {
        'telegram': {
            'class': 'telegram_handler.TelegramHandler',
            'token': 'your token',
            'chat_id': 'chat id'
        }
    },
    'loggers': {
        'my_logger': {
            'handlers': ['telegram'],
            'level': 'DEBUG'
        }
    }
}

Formatting

Currently the format of sent messages is very simple: <code>%(asctime)s</code> <b>%(levelname)s</b>\nFrom %(name)s:%(funcName)s\n%(message)s Exception traceback will be formatted as pre-formatted fixed-width code block. (https://core.telegram.org/bots/api#html-style)

If you want to tweak it, configure a telegram_handler.HtmlFormatter with your desired format string. Using a dictConfig:

...
{
    'formatters': {
        'telegram': {
            'class': 'telegram_handler.HtmlFormatter',
            'fmt': '%(levelname)s %(message)s'
        }
    }
    'handlers': {
        'telegram': {
            'class': 'telegram_handler.TelegramHandler',
            'formatter': 'telegram',
            'token': 'your token',
            'chat_id': 'chat id'
        }
    }
}
...

If you wish, you can enable emoji symbols in HtmlFormatter. Just specify use_emoji=True in HtmlFormatter settings. This will add to levelname a ⚪ for DEBUG, 🔵 for INFO, and 🔴 for WARNING and ERROR levels.

Proxy

In case if you have to use this package inside the country where Telegram servers are blocked by gowrnment you can specify proxy urls in config. Using a dictConfig:

...
{
    'handlers': {
        'telegram': {
            'class': 'telegram_handler.TelegramHandler',
            'formatter': 'telegram',
            'token': 'your token',
            'chat_id': 'chat id',
            'proxies': {
                'http': 'socks5://user:pass@host:port',
                'https': 'socks5://user:pass@host:port'
            }
        }
    }
}
...

Important! If you plan to use socks proxy make sure you have requests package with socks support installed:

pip install requests[socks]

PRAGMA foreign_keys = off; BEGIN TRANSACTION; COMMIT TRANSACTION; PRAGMA foreign_keys = on;

Subscribe to our newsletter