# Telegram Bot Logger
[](https://pypi.org/project/telegram-bot-logger/)
[](https://pypi.org/project/telegram-bot-logger/)
Telegram Bot Logger is a Python library that allows you to send logging logs to Telegram using the Bot API. It simplifies the process of integrating Telegram bot notifications into your Python applications, making it easy to monitor and manage your application's logs.

## Installation
You can install `telegram_bot_logger` using pip:
```bash
pip install telegram-bot-logger
```
## Usage
Replace `YOUR_BOT_TOKEN` and `chat_ids` with your actual bot token and chat IDs. You can obtain a bot token by creating a new bot on Telegram and obtaining it from the [BotFather](https://t.me/BotFather).
```python
import telegram_bot_logger
import logging
logger = logging.getLogger("telegram_bot_logger_example")
handler = telegram_bot_logger.TelegramMessageHandler(
bot_token = "YOUR_BOT_TOKEN", # Required; bot's token from @BotFather
chat_ids = [
12345678,
-1001234567890, # For group chat id, make sure you pass the chat id as integer
"@username"
], # Required; you can pass id as integer or username as string
api_server = telegram_bot_logger.api_server.TelegramAPIServer(
base = "https://api.telegram.org/bot{bot_token}/{method}"
), # Optional; set by default
format_type = "text" or "TEXT" or telegram_bot_logger.formatters.FormatType.TEXT, # Optional; also can be "DOCUMENT", by default it is "TEXT"
document_name_strategy = "timestamp" or "TIMESTAMP" or telegram_bot_logger.formatters.DocumentNameStrategy.TIMESTAMP, # Optional; used to define documents' names; also can be "ARGUMENT", by default it is "TIMESTAMP"
proxies = {
"http://": "http://localhost:8080"
} or "http://localhost:8080", # Optional; "dict[scheme, url]" or just "url", please see httpx's supported proxy types
formatter = formatters.TelegramHTMLTextFormatter(), # Optional; you can create your own class inherited from formatters.TelegramBaseFormatter and pass it
additional_body = {
"reply_to_message_id": 1
} # Optional; additional request body on sendMessage and sendDocument
)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.debug("debug-message")
# Or:
logger.debug("debug-message", extra={"document_name": 123}) # 123 is an argument; to use this feature you need to set `format_type = formatters.FormatType.DOCUMENT` and `document_name_strategy = formatters.DocumentNameStrategy.ARGUMENT` while initiating TelegramMessageHandler
```
It is highly recommend using string formatters in log messages, as these are not expanded if the logging level is not high enough,
making difference in performance critical applications:
```python
# Use string formatter to avoid formatting an excessive log message argument
long_list = list(range(1000))
logger.info("My long list is: %s", long_list)
```
You can also show tracebacks:
```python
try:
raise RuntimeError("Ooops I did it again")
except Exception as e:
logger.exception(e)
```

## Closing the handler
This logging handler creates a daemon background thread, as it uses Python's internal [QueueHandler](https://docs.python.org/3/library/logging.handlers.html#queuehandler). The thread is closed with Python's `atexit` handler. However for some applications, like pytest, to cleanly shut down, you may need to shutdown the handler manually.
```python
# Release any background threads created by the Telegram logging handler
handler.close()
```
## More examples
Here is an example how to customise Telegram output a bit:
```python
import logging
import telegram_bot_logger
from telegram_bot_logger.formatters import TelegramHTMLTextFormatter
# Fine tune our Telegram chat output
formatter = TelegramHTMLTextFormatter()
formatter._EMOTICONS[logging.DEBUG] = "📜" # Patch in the emoticon by logging level
formatter._TAG_FORMAT = "" # Disable tags in the output
formatter._HEADER_FORMAT = "<pre>{emoticon} {message}{description}</pre>" # Disable line number and module name in the output
telegram_handler = telegram_bot_logger.TelegramMessageHandler(
bot_token = "YOUR_BOT_TOKEN",
chat_ids = [
-1001234567890,
],
format_type = "text",
formatter = formatter,
level = logging.INFO
)
logging.getLogger().addHandler(telegram_handler)
```
## Stay Updated
For the latest news and updates, follow my [Telegram Channel](https://aryn.sek.su/tg/dev).
Raw data
{
"_id": null,
"home_page": "https://github.com/arynyklas/telegram_bot_logger",
"name": "telegram-bot-logger",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "telegram, logger, logging, telegram_logger",
"author": "Aryn Yklas",
"author_email": "arynyklas@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b3/7e/8b5d376b5234ebf7fdcde62e22fb6dc8a4bb51897293a5471c21ddfacec7/telegram_bot_logger-0.0.4.post0.tar.gz",
"platform": null,
"description": "# Telegram Bot Logger\n\n[](https://pypi.org/project/telegram-bot-logger/)\n[](https://pypi.org/project/telegram-bot-logger/)\n\nTelegram Bot Logger is a Python library that allows you to send logging logs to Telegram using the Bot API. It simplifies the process of integrating Telegram bot notifications into your Python applications, making it easy to monitor and manage your application's logs.\n\n\n\n## Installation\n\nYou can install `telegram_bot_logger` using pip:\n\n```bash\npip install telegram-bot-logger\n```\n\n\n## Usage\n\nReplace `YOUR_BOT_TOKEN` and `chat_ids` with your actual bot token and chat IDs. You can obtain a bot token by creating a new bot on Telegram and obtaining it from the [BotFather](https://t.me/BotFather).\n\n```python\nimport telegram_bot_logger\n\nimport logging\n\n\nlogger = logging.getLogger(\"telegram_bot_logger_example\")\n\nhandler = telegram_bot_logger.TelegramMessageHandler(\n bot_token = \"YOUR_BOT_TOKEN\", # Required; bot's token from @BotFather\n chat_ids = [\n 12345678,\n -1001234567890, # For group chat id, make sure you pass the chat id as integer\n \"@username\"\n ], # Required; you can pass id as integer or username as string\n api_server = telegram_bot_logger.api_server.TelegramAPIServer(\n base = \"https://api.telegram.org/bot{bot_token}/{method}\"\n ), # Optional; set by default\n format_type = \"text\" or \"TEXT\" or telegram_bot_logger.formatters.FormatType.TEXT, # Optional; also can be \"DOCUMENT\", by default it is \"TEXT\"\n document_name_strategy = \"timestamp\" or \"TIMESTAMP\" or telegram_bot_logger.formatters.DocumentNameStrategy.TIMESTAMP, # Optional; used to define documents' names; also can be \"ARGUMENT\", by default it is \"TIMESTAMP\"\n proxies = {\n \"http://\": \"http://localhost:8080\"\n } or \"http://localhost:8080\", # Optional; \"dict[scheme, url]\" or just \"url\", please see httpx's supported proxy types\n formatter = formatters.TelegramHTMLTextFormatter(), # Optional; you can create your own class inherited from formatters.TelegramBaseFormatter and pass it\n additional_body = {\n \"reply_to_message_id\": 1\n } # Optional; additional request body on sendMessage and sendDocument\n)\n\nlogger.setLevel(logging.DEBUG)\n\nlogger.addHandler(handler)\n\n\nlogger.debug(\"debug-message\")\n# Or:\nlogger.debug(\"debug-message\", extra={\"document_name\": 123}) # 123 is an argument; to use this feature you need to set `format_type = formatters.FormatType.DOCUMENT` and `document_name_strategy = formatters.DocumentNameStrategy.ARGUMENT` while initiating TelegramMessageHandler\n```\n\nIt is highly recommend using string formatters in log messages, as these are not expanded if the logging level is not high enough,\nmaking difference in performance critical applications:\n\n```python\n# Use string formatter to avoid formatting an excessive log message argument \nlong_list = list(range(1000))\nlogger.info(\"My long list is: %s\", long_list)\n```\n\nYou can also show tracebacks:\n\n```python\ntry:\n raise RuntimeError(\"Ooops I did it again\")\nexcept Exception as e:\n logger.exception(e)\n```\n\n\n\n## Closing the handler\n\nThis logging handler creates a daemon background thread, as it uses Python's internal [QueueHandler](https://docs.python.org/3/library/logging.handlers.html#queuehandler). The thread is closed with Python's `atexit` handler. However for some applications, like pytest, to cleanly shut down, you may need to shutdown the handler manually.\n\n```python\n# Release any background threads created by the Telegram logging handler\nhandler.close()\n```\n\n## More examples\n\nHere is an example how to customise Telegram output a bit:\n\n```python\nimport logging\nimport telegram_bot_logger\nfrom telegram_bot_logger.formatters import TelegramHTMLTextFormatter\n\n# Fine tune our Telegram chat output\nformatter = TelegramHTMLTextFormatter()\nformatter._EMOTICONS[logging.DEBUG] = \"\ud83d\udcdc\" # Patch in the emoticon by logging level\nformatter._TAG_FORMAT = \"\" # Disable tags in the output\nformatter._HEADER_FORMAT = \"<pre>{emoticon} {message}{description}</pre>\" # Disable line number and module name in the output\n\ntelegram_handler = telegram_bot_logger.TelegramMessageHandler(\n bot_token = \"YOUR_BOT_TOKEN\",\n chat_ids = [\n -1001234567890,\n ],\n format_type = \"text\",\n formatter = formatter,\n level = logging.INFO\n)\n\nlogging.getLogger().addHandler(telegram_handler)\n```\n\n## Stay Updated\n\nFor the latest news and updates, follow my [Telegram Channel](https://aryn.sek.su/tg/dev).\n",
"bugtrack_url": null,
"license": null,
"summary": "It is a library that allows you to send logging logs to Telegram",
"version": "0.0.4.post0",
"project_urls": {
"Homepage": "https://github.com/arynyklas/telegram_bot_logger"
},
"split_keywords": [
"telegram",
" logger",
" logging",
" telegram_logger"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "0fce8bd35a661129ae73afcfa4aa0995f1615652cd148afdea1eef5060506da6",
"md5": "a024784ee3b8a15b76a048011408bc3e",
"sha256": "917ecf8903aea0f49419b1e4bfb82fcc2b721f38c57c643d4f6b48559c40e02c"
},
"downloads": -1,
"filename": "telegram_bot_logger-0.0.4.post0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a024784ee3b8a15b76a048011408bc3e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8806,
"upload_time": "2025-02-06T18:39:21",
"upload_time_iso_8601": "2025-02-06T18:39:21.226989Z",
"url": "https://files.pythonhosted.org/packages/0f/ce/8bd35a661129ae73afcfa4aa0995f1615652cd148afdea1eef5060506da6/telegram_bot_logger-0.0.4.post0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "b37e8b5d376b5234ebf7fdcde62e22fb6dc8a4bb51897293a5471c21ddfacec7",
"md5": "7d49df986d69809ce866a925863b29b9",
"sha256": "ffebfe5c8abff27e86bece99d42a774afee564ba46e48181deac3901d0ea9e7e"
},
"downloads": -1,
"filename": "telegram_bot_logger-0.0.4.post0.tar.gz",
"has_sig": false,
"md5_digest": "7d49df986d69809ce866a925863b29b9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 9276,
"upload_time": "2025-02-06T18:39:22",
"upload_time_iso_8601": "2025-02-06T18:39:22.623233Z",
"url": "https://files.pythonhosted.org/packages/b3/7e/8b5d376b5234ebf7fdcde62e22fb6dc8a4bb51897293a5471c21ddfacec7/telegram_bot_logger-0.0.4.post0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-06 18:39:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "arynyklas",
"github_project": "telegram_bot_logger",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "httpx",
"specs": [
[
">=",
"0.28"
]
]
}
],
"lcname": "telegram-bot-logger"
}