Skip to main content

Python logging handler which posts to slack

Project description

This package provides logging.Handler implementations that will send message to a slack channel. Supports updated slack.WebClient API released in 2020.

Authorization

See https:://api.slack.com to for information on setting up app and get an authorization token. Only a “bot” token is required to use SlackHandler, however, the unittest is more complete with a “user” token.

Limitations

How often messages can be sent is limited by slack. See https://api.slack.com/docs/rate-limits. By default, this handler only sends accumulated messages every 60 seconds. That can be adjusted by the optional update argument to SlackHandler.

send_remaining() should be called prior to program exit to send any remaining messages.

Example

from nmrbox_slack import SlackHandler
token = 'your token here'
channel = 'your channel name'
handler = SlackHandler(token, channel)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
example_logger = logging.getLogger("Test Logger")
example_logger.propagate = False
example_logger.addHandler(handler)
example_logger.setLevel(logging.INFO)
example_logger.info("Then shalt thou use Python three, no more, no less.")
...
# at end of program
handler.send_remaining()

Variants

Two versions are currently supported.

  • SlackHandler connects to slack upon creation and validates the token and channel name.

  • LazySlackHandler does not attempt to connect to slack until the first message is sent.

SlackHandler is recommended if it is known messages are to be sent. LazySlackHandler is available to reduce overhead of processes which typically do not send messages.

Additional channels

Sending to additional channels may be done more efficiently with the SlackHandler.additional_channel_handler() method.

from nmrbox_slack.slacklogger import SlackHandler
token = 'your token here'
channel = 'your channel name'
handler = SlackHandler(token, channel)
second_handler = heandler.additional_channel_handler('second channel name')

From mapping

from_mapping(mapping:Mapping,,lazy:bool)* will return a SlackHandler or LazySlackHandler from a mapping:

{'slack:
    {
    'channel':<channel name>,
    'token file': <file with token in it>
    }
}

Release history

2.0 New API from slack 2.2.2 Add pagination

Funding acknowledgment

This work supported by National Institutes of Health (NIH) / National Institute of General Medical Sciences (NIGMS), grant 1P41GM111135.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

slack_webclient_logger-2.2.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file slack_webclient_logger-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: slack_webclient_logger-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/65.6.3 requests-toolbelt/0.8.0 tqdm/4.64.1 CPython/3.8.10

File hashes

Hashes for slack_webclient_logger-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d2f3243d70594f637ca398e5a8b3ec9cec2aa23c7f13b8daf316e60a923a2f7b
MD5 93514fa58206636e8be0230a0663022c
BLAKE2b-256 b864a082fce71455030e9250c8833496b32c1fb518fa0790b737a67be5bb550a

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