Skip to content

Instantly share code, notes, and snippets.

@tylerpace
Last active September 8, 2017 20:10

Revisions

  1. tylerpace revised this gist Sep 8, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion HipchatHandler.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    import json
    import logging
    import requests
    from requests.auth import HTTPBasicAuth


    class HipChatHandler(logging.Handler):
  2. tylerpace revised this gist Sep 8, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions HipchatHandler.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    import json
    import logging
    import requests
    from requests.auth import HTTPBasicAuth


    class HipChatHandler(logging.Handler):

    def __init__(self, auth_token, room_id, level=logging.INFO):
  3. tylerpace renamed this gist Sep 8, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. tylerpace created this gist Sep 8, 2017.
    32 changes: 32 additions & 0 deletions HipchatHandler
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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)