Created
March 28, 2014 12:02
-
-
Save rickmak/9831114 to your computer and use it in GitHub Desktop.
Python logger for slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import json | |
import requests | |
class SlackHandler(logging.Handler): # Inherit from logging.Handler | |
def __init__(self, domain, token, channel, name='LOGGER'): | |
logging.Handler.__init__(self) | |
self.domain = domain | |
self.token = token | |
self._url = "https://%s/services/hooks/incoming-webhook?token=%s" % \ | |
(self.domain, self.token) | |
self.channel = channel | |
self.name = name | |
def emit(self, record): | |
payload = { | |
'channel': self.channel, | |
'username': self.name, | |
'text': record.message | |
} | |
r = requests.post( | |
self._url, | |
headers={ | |
'content-type': 'application/json' | |
}, | |
data=json.dumps(payload) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rick, what if you wrap this properly to a package and open sources it? We can then send it to slack maybe they will give us free chat room XD