Skip to main content

log_to_slack

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

Original work by Mathias Ose https://github.com/mathiasose/slacker_log_handler

Installation

pip install log-to-slack

Options

api_key (required)

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

channel (required)

Set which channel id you want to post to, e.g. “C0XXXXXXXXX”.

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.

Example Python logging handler

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

import logging
import sys
from datetime import datetime
from log_to_slack import SlackLogHandler, NoStacktraceFormatter

logger = logging.getLogger("Daily Report")
logger.setLevel(logging.DEBUG)

""" Adding slack handler """
slack_handler = SlackerLogHandler(
    "xoxb-XXXXX-XXX",  # slack api token
    "C0XXXXXXXXX", # channel id not channel name
    stack_trace=True,
    fail_silent=True,
)
formatter = NoStacktraceFormatter(
    "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
slack_handler.setFormatter(formatter)
slack_handler.setLevel(logging.ERROR)
logger.addHandler(slack_handler)

if __name__ == "__main__":
    logger.debug("Debug message to slack!")
    logger.info("Info message to slack!")
    logger.warning("Warning message to slack!")

    try:
        x = 5 / 0
    except:
        logger.exception("Exception message to slack!")
        # # you can also use the below statement to log error with trace info
        # logger.error("Error message to slack!", exc_info=True)

Slack message formatting

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

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

License

Apache 2.0

Slacker is also under Apache 2.0.

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

Metadata

Release files for log-to-slack 1.8.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 log-to-slack 1.8.1
File Size Uploaded Provenance
log_to_slack-1.8.1.tar.gz 10.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for log-to-slack 1.8.1
File Interpreter ABI Platform
log_to_slack-1.8.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 17.6 kB

Release files / log_to_slack-1.8.1.tar.gz

Download URL log_to_slack-1.8.1.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1dd484675054fcd57090068f6ed40ce4c08bead5bd0457210e3999b4bd372dec
BLAKE2b-256 checksum
How to use checksums
2b242294506b15d2bc40dd23679df0fe2e888a28c99ccafe0c35da329b9f6cdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.4

Release files / log_to_slack-1.8.1-py2.py3-none-any.whl

Download URL log_to_slack-1.8.1-py2.py3-none-any.whl
Size 7.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f65edf9526c20f2cfdd6c04f0eb65ed12476f62b21a76c7a0180ca43deae13d1
BLAKE2b-256 checksum
How to use checksums
ba85f4c92f47ebabde160f2d0f454ea39a65dbf2689dacf122116170c1eb69a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.4

Security for log-to-slack 1.8.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.8.1 This release

2 release files No provenance

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