Skip to main content

<img src="https://github.com/NSb0y/loggerbot/raw/main/images/loggerbot.png" width="288"/>

LoggerBot

© N.Sikharulidze (https://ubix.pro/)

LoggerBot is a Telegram logging library that allows you to send log messages to one or multiple Telegram chats using a bot. It provides an easy-to-use interface for logging various types of messages and includes features like rate limiting and message queuing.

Table of Contents

Features

  • Send log messages to one or multiple Telegram chats
  • Support for different log levels: debug, info, warning, error, critical, alert, and complete
  • Rate limiting to prevent excessive API calls
  • Message queuing for smooth operation
  • Easy integration with existing Python logging
  • Backward compatibility with the LogBot alias
  • HTML formatting support for log messages

Examples

HTML Formatting log example

<img src="https://github.com/NSb0y/loggerbot/raw/main/images/alertExample2.png" width="248"/>

Regular log examples

<img src="https://github.com/NSb0y/loggerbot/raw/main/images/alertExample.png" width="248"/>

Installation

You can install LoggerBot using pip:

pip install loggerbot

Pypi.org (https://pypi.org/project/loggerbot/)

Usage

Here's a basic example of how to use LoggerBot:

from loggerbot import TelegramLoggingBot

# Initialize the bot with your Telegram bot token and chat ID(s)
log_bot = TelegramLoggingBot("YOUR_BOT_TOKEN", ["CHAT_ID_1", "CHAT_ID_2"])

# Send log messages
log_bot.info("This is an informational message")
log_bot.warning("This is a warning message")
log_bot.error("An error occurred")
log_bot.alert("This is an important alert")
log_bot.complete("Task completed successfully")

Advanced Usage

Rate Limiting

LoggerBot includes built-in rate limiting to prevent excessive API calls. You can customize the rate limit when initializing the bot:

log_bot = TelegramLoggingBot("YOUR_BOT_TOKEN", "CHAT_ID", rate_limit=20, time_window=60)

This sets a limit of 20 messages per 60 seconds.

Setting Log Level

You can set the minimum log level for the bot:

import logging
log_bot.set_log_level(logging.DEBUG)

Using with Python's logging module

LoggerBot can be integrated with Python's built-in logging module:

import logging
from loggerbot import TelegramLoggingBot

# Create a logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# Create a TelegramLoggingBot instance
tg_handler = TelegramLoggingBot("YOUR_BOT_TOKEN", "CHAT_ID")

# Add the TelegramLoggingBot as a handler to the logger
logger.addHandler(tg_handler)

# Now you can use the logger as usual
logger.info("This message will be sent to Telegram")

HTML Formatting for Log Messages

LoggerBot supports HTML formatting in log messages, allowing you to enhance the readability and structure of your logs in Telegram. Here are some examples of how to use HTML formatting:

# Bold text
log_bot.info("This is an <b>important</b> message")

# Italic text
log_bot.warning("Please <i>be cautious</i> when proceeding")

# Code formatting
log_bot.error("An error occurred: <code>ValueError: Invalid input</code>")

# Underlined text
log_bot.alert("This requires <u>immediate attention</u>")

# Combining multiple formats
log_bot.complete("Task <b><i>successfully</i></b> completed")

# Using line breaks
log_bot.info("This is a multi-line message.<br>It has two lines.")

# Creating a list
log_bot.info("Steps to reproduce:<br>1. Open the app<br>2. Click on settings<br>3. Select 'Advanced'")

# Adding a hyperlink
log_bot.info("For more information, visit our <a href='https://example.com'>documentation</a>")

Note: Make sure to use HTML entities for special characters like < (&lt;), > (&gt;), and & (&amp;) when they're not part of HTML tags.

Register a Telegram Bot

<img src="https://github.com/NSb0y/loggerbot/raw/main/images/tg2.webp" width="188"/>

To register a Telegram bot and obtain an API key, follow these steps:

  1. Open the Telegram app and search for BotFather.
  2. Start a chat with BotFather and use the /newbot command to create a new bot.
  3. Follow the instructions to name your bot and get a unique username.
  4. Once your bot is created, you will receive a Bot Token. This token is used to authenticate your bot with the Telegram API.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Release files for loggerbot 3.8.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for loggerbot 3.8.8
File Size Uploaded
loggerbot-3.8.8.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for loggerbot 3.8.8
File Interpreter ABI Platform
loggerbot-3.8.8-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / loggerbot-3.8.8.tar.gz

Download URL loggerbot-3.8.8.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
50f154ca614430698c456e2963b2340b0f08f050a4bee46203bfe9c6e7cf6e00
BLAKE2b-256 checksum
How to use checksums
29f85be8bc2a01c114d88c87fcb2ea7c56adb9e3132c648c584f36c7c30b2f73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / loggerbot-3.8.8-py3-none-any.whl

Download URL loggerbot-3.8.8-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d31303911f58f67a6baefabcfb859f68e03224e8f0011241bf3ed1a32ae907ed
BLAKE2b-256 checksum
How to use checksums
4fb5405e4411b82dc8d8ba6107f3d3287ab2366cf584b92b2d2ee71b8b389840
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

3.8.8 This release

2 release files

2.8.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page