Skip to main content

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.

Release files for logcaster 0.5.1

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

Source distribution (sdist)

Source distribution for logcaster 0.5.1
File Size Uploaded
logcaster-0.5.1.tar.gz 10.5 kB Details

Built distribution (wheel)

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

Total release size:23.7 kB

Release files / logcaster-0.5.1.tar.gz

Download URL logcaster-0.5.1.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ff6b96a8ff91330e6c7319f83c4d90c0865b32595369c7ddb08fbbe76e08bd95
BLAKE2b-256 checksum
How to use checksums
333f56d6f51cbb839aa8be35823237415c9797188195edf2fffdd32351bb7c66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.11.11 Linux/6.8.0-63-generic

Release files / logcaster-0.5.1-py3-none-any.whl

Download URL logcaster-0.5.1-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cdad307aab7ed26de0c3e4365729e58d52d443455dc5846adc7658ec49ce4ff
BLAKE2b-256 checksum
How to use checksums
0725a1f718f6c6d1d720dda9f1a7999cca235e60b9efe05d410d2ecb0aba3c93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.11.11 Linux/6.8.0-63-generic

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

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