Skip to content

Instantly share code, notes, and snippets.

@mannysz
Forked from bwhaley/example_usage.py
Created July 7, 2016 01:44
Show Gist options
  • Select an option

  • Save mannysz/4ac618dead982f909f4b24694282a20c to your computer and use it in GitHub Desktop.

Select an option

Save mannysz/4ac618dead982f909f4b24694282a20c to your computer and use it in GitHub Desktop.

Revisions

  1. @bwhaley bwhaley revised this gist Mar 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example_usage.py
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,4 @@

    logging.config.dictConfig(sumologger.LOGGING)
    logger = logging.getLogger("sumologger")
    logger.debug("sumologger")
    logger.debug("Nifty log message")
  2. @bwhaley bwhaley revised this gist Mar 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example_usage.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,6 @@
    import sumologger
    from sumologger import SumoHTTPHandler

    logging.config.dictConfig(loggingconfig.LOGGING)
    logging.config.dictConfig(sumologger.LOGGING)
    logger = logging.getLogger("sumologger")
    logger.debug("sumologger")
  3. @bwhaley bwhaley revised this gist Mar 6, 2014. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions example_usage.py
    Original file line number Diff line number Diff line change
    @@ -4,5 +4,5 @@
    from sumologger import SumoHTTPHandler

    logging.config.dictConfig(loggingconfig.LOGGING)
    logger = logging.getLogger('test')
    logger.debug("test")
    logger = logging.getLogger("sumologger")
    logger.debug("sumologger")
    4 changes: 2 additions & 2 deletions sumologger.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # http://help.sumologic.com/Help/Default.htm#Uploading_Files_to_an_HTTP_Source.htm

    class SumoHTTPHandler(logging.Handler):
    def __init__(self, url, host='collectors.sumologic.com', name=None, compressed=False):
    def __init__(self, url, host="collectors.sumologic.com", name=None, compressed=False):
    """
    Similar to HTTPHandler but with some custom Sumo-friendly headers
    """
    @@ -21,7 +21,7 @@ def emit(self, record):
    h = httplib.HTTPS(host)
    url = self.url
    data = urllib.quote(self.format(record))
    sep = '?'
    sep = "?"
    url = url + "%c%s" % (sep, data)
    h.putrequest("GET", url)
    h.putheader("Host", host)
  4. @bwhaley bwhaley revised this gist Mar 6, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions sumologger.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    import logging.handlers

    # GET method for sending data to Sumo HTTP Source
    # http://help.sumologic.com/Help/Default.htm#Uploading_Files_to_an_HTTP_Source.htm

    class SumoHTTPHandler(logging.Handler):
    def __init__(self, url, host='collectors.sumologic.com', name=None, compressed=False):
    """
  5. @bwhaley bwhaley created this gist Mar 6, 2014.
    8 changes: 8 additions & 0 deletions example_usage.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    import logging
    import logging.config
    import sumologger
    from sumologger import SumoHTTPHandler

    logging.config.dictConfig(loggingconfig.LOGGING)
    logger = logging.getLogger('test')
    logger.debug("test")
    51 changes: 51 additions & 0 deletions sumologger.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    import logging.handlers

    class SumoHTTPHandler(logging.Handler):
    def __init__(self, url, host='collectors.sumologic.com', name=None, compressed=False):
    """
    Similar to HTTPHandler but with some custom Sumo-friendly headers
    """
    logging.Handler.__init__(self)
    self.host = host
    self.url = url
    self.name = name
    self.compressed = compressed

    def emit(self, record):
    try:
    import httplib, urllib
    host = self.host
    h = httplib.HTTPS(host)
    url = self.url
    data = urllib.quote(self.format(record))
    sep = '?'
    url = url + "%c%s" % (sep, data)
    h.putrequest("GET", url)
    h.putheader("Host", host)
    if self.compressed:
    h.putheader("Content-Encoding", "gzip")
    if self.name:
    h.putheader("X-Sumo-Name", self.name)
    h.endheaders()
    h.getreply() #can't do anything with the result
    except (KeyboardInterrupt, SystemExit):
    raise
    except:
    self.handleError(record)

    LOGGING = {
    'version': 1,
    'handlers': {
    'sumo':{
    'level': "DEBUG",
    'class': '__main__.SumoHTTPHandler',
    'url': '/receiver/v1/.....', #Replace with your custom Sumo Hosted URL
    }
    },
    'loggers': {
    'sumologger': {
    'handlers': ['sumo'],
    'level': 'DEBUG'
    }
    }
    }