Skip to content

Instantly share code, notes, and snippets.

@PaulNendick
Forked from bwhaley/example_usage.py
Last active August 29, 2015 14:13
Show Gist options
  • Save PaulNendick/526289664f8a1d6c1532 to your computer and use it in GitHub Desktop.
Save PaulNendick/526289664f8a1d6c1532 to your computer and use it in GitHub Desktop.
import logging
import logging.config
import sumologger
from sumologger import SumoHTTPHandler
logging.config.dictConfig(sumologger.LOGGING)
logger = logging.getLogger("sumologger")
logger.debug("Nifty log message")
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):
"""
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'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment