Skip to main content

pyTelegramLogger

Description

This package provides handlers to send logging messages to telegram chats. It uses a separate thread for handling messages (see logging.QueueHandler for details). You can send messages to multiple chats, including big messages (over 4096 chars). Big messages will be split into several parts and tagged by unique hashtag for current log record.

Installation

$ pip install pyTelegramLogger
$ pipenv install pyTelegramLogger

Usage

  1. Configure logger
LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'telegram': {
            'class': 'telegram_logger.TelegramHandler',
            'chat_ids': [123456, 123456789],
            'token': 'bot_token,
        },
    },
    'loggers': {
        'telegram': {
            'handlers': ['telegram'],
        }
    }
}
  1. Usage
# Run once at startup:
logging.config.dictConfig(LOGGING_CONFIG)

# Include to send logging messages to telegram
logger = logging.getLogger('telegram')

logger.warning('Warning message!')

FAQ

1. Can I use my own formatter class for messages?

Yes you can. You can inherit from base class telegram_logger.TelegramFormatter and define how to send big messages (over 4096 chars) in method format_by_fragments(self, record: logging.LogRecord, start: int=0) -> List[str]. Also you can use your own formatter class. Then configure using standart configuration dictionary schema:

LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'myFormattrer': {
            'class': 'MyFormatterClass',
            'fmt': '%(levelname)_%(name) : %(funcName)'
        }
    }
    'handlers': {
        'telegram': {
            'class': 'telegram_logger.TelegramHandler',
            'chat_ids': [123456, 123456789],
            'token': 'bot_token,
            'formatter': 'myFormattrer',
        },
    },
    'loggers': {
        'telegram': {
            'handlers': ['telegram'],
        }
    }
}

2. What if I don't want to send messages to telegram during development?

Sometimes, when you just develope you app and you dont want to send log messages to telegram, but want to control it use telegram_logger.TelegramStreamHandler(cht_ids, token, stream=None) which streams dict with message text and params instead of send it to telegram.

3. Can I use a proxy for sending messages?

Yes you can. You can specify proxy in key of config proxies using dict format as requests:

proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'http://10.10.1.10:1080',
}

LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'telegram': {
            'class': 'telegram_logger.TelegramHandler',
            'chat_ids': [123456, 123456789],
            'token': 'bot_token,
            'proxies': proxies,
        },
    },
    'loggers': {
        'telegram': {
            'handlers': ['telegram'],
        }
    }
}

Metadata

Release files for pyTelegramLogger 1.0.4

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

Source distribution (sdist)

Source distribution for pyTelegramLogger 1.0.4
File Size Uploaded
pyTelegramLogger-1.0.4.tar.gz 8.0 kB Details

Built distribution (wheel)

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

Total release size: 15.8 kB

Release files / pyTelegramLogger-1.0.4.tar.gz

Download URL pyTelegramLogger-1.0.4.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
313b3aa2aed283a65d255e2353b0bf34bfc2df547240844def30cb65a3f203e3
BLAKE2b-256 checksum
How to use checksums
6fa1e6d7d95fef33590b8cff32bdd8c94459a0e3529356ad53d57f817fd44832
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / pyTelegramLogger-1.0.4-py3-none-any.whl

Download URL pyTelegramLogger-1.0.4-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0845c7651a367bce50a407059006a1dfbd2d84a7f9f0cb713b527053de73eb0
BLAKE2b-256 checksum
How to use checksums
08f1a8eaffb58f0f504232b4fc45a2c67b9e6808139c9ea0d255250cdbbbcdc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.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