Skip to main content
https://img.shields.io/pypi/v/slacker_log_handler.svg?style=flat-square https://img.shields.io/pypi/wheel/slacker_log_handler.svg?style=flat-square https://img.shields.io/pypi/format/slacker_log_handler.svg?style=flat-square https://img.shields.io/pypi/pyversions/slacker_log_handler.svg?style=flat-square https://img.shields.io/pypi/status/slacker_log_handler.svg?style=flat-square

Python log handler that posts to a Slack channel. Posts to the Slack API using https://slack.dev/python-slack-sdk.

Created with the intention of using for a Internal project, but some effort has been made to make it generic enough that any Python project could use it.

Installation

pip install slackclient-log-handler

Options

api_token (required)

Generate a key at https://api.slack.com/

channel (required)

Set which channel you want to post to, e.g. “#general”.

username

The username that will post to Slack. Defaults to “Python logger”.

icon_url

URL to an image to use as the icon for the logger user

icon_emoji

emoji to use as the icon. Overrides icon_url. If neither icon_url nor icon_emoji is set, :heavy_exclamation_mark: will be used.

fail_silent

Defaults to False. If your API key is invalid or for some other reason the API call returns an error, this option will silently ignore the API error. If you enable this setting, make sure you have another log handler that will also handle the same log events, or they may be lost entirely.

Django configuration

Logging reference: https://docs.djangoproject.com/en/stable/topics/logging/

This example will send INFO and ERRORS to Slack, as well as errors to admin emails.

  • Set SLACK_API_KEY in your settings module.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'slack-error': {
            'level': 'ERROR',
            'api_token': SLACK_API_KEY,
            'class': 'slackclient_log_handler.SlackclientLogHandler',
            'channel': '#general'
        },
        'slack-info': {
            'level': 'INFO',
            'api_token': SLACK_API_KEY,
            'class': 'slackclient_log_handler.SlackclientLogHandler',
            'channel': '#general'
        },
        'loggers': {
            'django.request': {
                'handlers': ['mail_admins', 'slack-error', 'slack-info'],
                'level': 'ERROR',
                'propagate': True,
            },
        }
    }
}

Example Python logging handler

This is how you use slackclient_log_handler as a regular Python logging handler. This example will send a error message to a slack channel.

import logging
from slackclient_log_handler import SlackclientLogHandler, NoStacktraceFormatter

# Create slack handler
slack_handler = SlackclientLogHandler('my-channel-token', 'my-channel-name')

# Create logger
logger = logging.getLogger('debug_application')
logger.addHandler(slack_handler)

# OPTIONAL: Define a log message formatter.
# If you have set stack_trace=True, any exception stack traces will be included as Slack message attachments.
# You therefore need to use NoStacktraceFormatter as a base to exclude the trace from the main message text.
formatter = NoStacktraceFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
slack_handler.setFormatter(formatter)

# Define the minimum level of log messages you want to send to Slack
slack_handler.setLevel(logging.DEBUG)

# Test logging
logger.error("Debug message from slack!")

Slack message formatting

This example use a subclass that will send a formatted message to a slack channel. Reference: https://api.slack.com/docs/message-formatting

class CustomLogHandler(SlackclientLogHandler):
    def build_msg(self, record):
        message = "> New message :\n" + record.getMessage()
        return message

License

Apache 2.0

Slack-sdk is also under MIT.

See also: https://api.slack.com/terms-of-service

Release files for slackclient-log-handler 0.1.2

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

Source distribution (sdist)

Source distribution for slackclient-log-handler 0.1.2
File Size Uploaded
slackclient_log_handler-0.1.2.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slackclient-log-handler 0.1.2
File Interpreter ABI Platform
slackclient_log_handler-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / slackclient_log_handler-0.1.2.tar.gz

Download URL slackclient_log_handler-0.1.2.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
72b8fa1f64992e43b5bfe2d60ce1b6b232f41d54232dd5df104b605abea6ad4a
BLAKE2b-256 checksum
How to use checksums
b8401f12bb28bdfc726cb8af283f7d51c029ff60ff8265b1b17763099db8d9eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.2 Linux/6.1.0-16-amd64

Release files / slackclient_log_handler-0.1.2-py3-none-any.whl

Download URL slackclient_log_handler-0.1.2-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e464d40e3c7d95956cee23234e0ee2bd9b30b7fd94f45221564402fc1e5cd3a7
BLAKE2b-256 checksum
How to use checksums
aafbca8f3a454da63cbd2ae8d33c91aba44fef4ffd08d0c3430117c9ede7025c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.2 Linux/6.1.0-16-amd64

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

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