Skip to content

Instantly share code, notes, and snippets.

@rickmak
Created March 28, 2014 12:02
Show Gist options
  • Save rickmak/9831114 to your computer and use it in GitHub Desktop.
Save rickmak/9831114 to your computer and use it in GitHub Desktop.
Python logger for slack
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)
)
@chpapa
Copy link

chpapa commented Mar 28, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment