Skip to main content

Posts log events to Slack via API

Project description

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

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

log_to_slack-1.8.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

log_to_slack-1.8.1-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file log_to_slack-1.8.1.tar.gz.

File metadata

  • Download URL: log_to_slack-1.8.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.4

File hashes

Hashes for log_to_slack-1.8.1.tar.gz
Algorithm Hash digest
SHA256 1dd484675054fcd57090068f6ed40ce4c08bead5bd0457210e3999b4bd372dec
MD5 d4a420552ee06e6c7a32a290edd0d2f8
BLAKE2b-256 2b242294506b15d2bc40dd23679df0fe2e888a28c99ccafe0c35da329b9f6cdc

See more details on using hashes here.

File details

Details for the file log_to_slack-1.8.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for log_to_slack-1.8.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f65edf9526c20f2cfdd6c04f0eb65ed12476f62b21a76c7a0180ca43deae13d1
MD5 9b1109333ca79d617253dfbed7171179
BLAKE2b-256 ba85f4c92f47ebabde160f2d0f454ea39a65dbf2689dacf122116170c1eb69a0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page