Python logging handler which posts to slack
This package provides logging.Handler implementations that will send message to a slack channel. Supports updated slack.WebClient API released in 2020.
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.
from nmrbox_slack.slacklogger 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()
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.
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')
This work supported by National Institutes of Health (NIH) / National Institute of General Medical Sciences (NIGMS), grant 1P41GM111135.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size slack_webclient_logger-2.2-py2-none-any.whl (5.3 kB)||File type Wheel||Python version py2||Upload date||Hashes View|
|Filename, size slack-webclient-logger-2.2.tar.gz (4.8 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for slack_webclient_logger-2.2-py2-none-any.whl
Hashes for slack-webclient-logger-2.2.tar.gz