Skip to content

Instantly share code, notes, and snippets.

@tylerpace
Last active September 8, 2017 20:10
Show Gist options
  • Save tylerpace/a545e444075b61b320a9d0be2d3ed7b0 to your computer and use it in GitHub Desktop.
Save tylerpace/a545e444075b61b320a9d0be2d3ed7b0 to your computer and use it in GitHub Desktop.
import json
import logging
import requests
class HipChatHandler(logging.Handler):
def __init__(self, auth_token, room_id, level=logging.INFO):
super(self.__class__, self).__init__(level)
self.api_uri = 'https://api.hipchat.com/v2/room/{room}/notification?auth_token={auth}'
self.auth_token = auth_token
self.room_id = room_id
self.options = {
logging.CRITICAL: {'color': 'red', 'notify': True},
logging.ERROR: {'color': 'red', 'notify': True},
logging.WARNING: {'color': 'yellow', 'notify': True},
logging.INFO: {'color': 'gray', 'notify': False},
logging.DEBUG: {'color': 'gray', 'notify': False},
logging.NOTSET: {'color': 'gray', 'notify': False}
}
def emit(self, record):
try:
body = record.__dict__
data = {
'message': self.format(record),
'message_format': 'text',
'color': self.options[body['levelno']]['color'],
'notify': self.options[body['levelno']]['notify'],
}
uri = self.api_uri.format(room=self.room_id, auth=self.auth_token)
requests.post(uri, data=json.dumps(data),
headers={'content-type': 'application/json'})
except: # pylint: disable=W0702
self.handleError(record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment