Skip to content

Instantly share code, notes, and snippets.

@lepture
Created December 15, 2014 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepture/eae4c7e2cec212589671 to your computer and use it in GitHub Desktop.
Save lepture/eae4c7e2cec212589671 to your computer and use it in GitHub Desktop.
Send logging to slack by webhook
# coding: utf-8
import json
import logging
import requests
class SlackHandler(logging.Handler):
def __init__(self, url, channel, username='error', emoji='scream'):
super(SlackHandler, self).__init__()
self._slack_url = url
self._slack_channel = channel
self._slack_username = username
self._slack_emoji = emoji
def emit(self, record):
msg = self.format(record)
url = self._slack_url
payload = json.dumps({
"channel": self._slack_channel,
"username": self._slack_username,
"text": msg,
"icon_emoji": ":%s:" % self._slack_emoji,
})
headers = {'Content-Type': 'application/json'}
requests.post(url, data=payload, headers=headers, timeout=5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment