Skip to main content

Logging handlers to send logs to discord and telegram

Project description

Logcaster

A package to send loggings to discord, telegram and whatever other location with the proposal of easily implements observability to small and lower coast applications

🛠🔧 Project in development process 🔧🛠

Available sources

  • Discord
  • Telegram

Features

  • easy to use.
  • natively supported by the built-in python logging package.
  • 'async' support.

Quick-start

Requirements

Install

# by defaults supports telegram setup
poetry add logcaster

Configure

Once installed, you need only set the environment vars (see: .env example file)

# .env
DISCORD__WEBHOOK_URL=https://discord.com/api/webhooks/<webhook.id>/<token>
TELEGRAM__BOT_TOKEN=<you bot token>
TELEGRAM__CHAT_ID=<the chat id that bot will send logs>

Usage

import logging

from logcaster.discord import DiscordFormatter, DiscordHandler
from logcaster.telegram import TelegramFormatter, TelegramHandler

logger = logging.getLogger(__name__)

dc_fmt = DiscordFormatter()
dc_hdl = DiscordHandler(level=logging.ERROR)

dc_hdl.setFormatter(dc_fmt)
logger.addHandler(dc_hdl)

tg_fmt = TelegramFormatter()
tg_hdl = TelegramHandler(level=logging.CRITICAL)

tg_hdl.setFormatter(tg_fmt)
logger.addHandler(tg_hdl)


logger.error('This will be sent to Discord only!')
logger.critical('This will be sent to both Telegram and Discord!')

Note: The default level is setting up to ERROR, it's highly recommended don't set a lower level, cause each emitted logging will make a request to the given source.

Filtering fields

dc_hdl.setFormatter(
    DiscordFormatter(include_fields=('asctime', 'levelname', 'msg'))
)
logger.critical('This will send only the included fields to Discord!')

tg_hdl.setFormatter(TelegramFormatter(exclude_fields=['asctime', 'levelname']))
logger.critical(
    'This will send all fields except the excluded ones to Telegram!'
)

using async handler

from logcaster.telegram import TelegramAsyncHandler
logger.addHandler(TelegramAsyncHandler())

Django example

# settings.py
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "telegram_fmt": {
            "class": "logcaster.telegram.TelegramFormatter",
        },
        "discord_fmt": {
            "class": "logcaster.discord.DiscordFormatter",
            "exclude_fields": ['funcName', 'lineno'],
        }
    },
    "handlers": {
        "telegram": {
            "class": "logcaster.telegram.TelegramHandler",
        },
        "discord": {
            "class": "logcaster.discord.DiscordHandler",
            "exclude_fields": ['funcName', 'lineno'],
        }
    },
    "loggers": {
        "logcaster": {
            "handlers": ["telegram", "discord"],
            "formatters": ["telegram_fmt", "discord_fmt"],
            "level": "ERROR",
            "propagate": False,
        },
    },
}

Contributing

  1. Fork this repo

  2. Clone your fork to your local machine:

    git clone https://github.com/<your-username>/logcaster.git
    cd logcaster
    
  3. Configure the upstream address to be able to fetch updates

    git remote add upstream https://github.com/LeandroDeJesus-S/logcaster.git
    
  4. Create a new brach to write your changes:

    git checkout -b feature/feature-name
    
  5. After make any changes be sure that the code is properly formatted and anything is broken:

    poetry run mypy ./logcaster
    poetry run ruff check ./logcaster
    poetry run ruff format ./logcaster
    poetry run pytest ./tests
    
    # or using make
    make check
    
  6. Having finished your changes, send a pull request with a good description about your work.

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

logcaster-0.5.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

logcaster-0.5.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file logcaster-0.5.1.tar.gz.

File metadata

  • Download URL: logcaster-0.5.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.11.11 Linux/6.8.0-63-generic

File hashes

Hashes for logcaster-0.5.1.tar.gz
Algorithm Hash digest
SHA256 ff6b96a8ff91330e6c7319f83c4d90c0865b32595369c7ddb08fbbe76e08bd95
MD5 d257e51f971fe2bc66c1b11a72ed7643
BLAKE2b-256 333f56d6f51cbb839aa8be35823237415c9797188195edf2fffdd32351bb7c66

See more details on using hashes here.

File details

Details for the file logcaster-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: logcaster-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.11.11 Linux/6.8.0-63-generic

File hashes

Hashes for logcaster-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cdad307aab7ed26de0c3e4365729e58d52d443455dc5846adc7658ec49ce4ff
MD5 ce95a9f3b38822874e39e334a9da85c9
BLAKE2b-256 0725a1f718f6c6d1d720dda9f1a7999cca235e60b9efe05d410d2ecb0aba3c93

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