Skip to content

Instantly share code, notes, and snippets.

@finomayato
Created August 21, 2018 23:06
Show Gist options
  • Select an option

  • Save finomayato/fd7cf6be49aaed39e947869eb6e06c21 to your computer and use it in GitHub Desktop.

Select an option

Save finomayato/fd7cf6be49aaed39e947869eb6e06c21 to your computer and use it in GitHub Desktop.
"""
handler.py
----------
Elasticsearch handler
"""
import logging
class ElasticsearchHandler(logging.Handler):
_es_instance = None
def __init__(self):
super(ElasticsearchHandler, self).__init__()
@classmethod
def from_es_instance(cls, es_instance):
handler_instance = cls()
handler_instance._es_instance = es_instance
return handler_instance
def _write_doc(self, doc):
self._es_instance.index(index='test-log', doc_type='log', body=doc)
def emit(self, record):
try:
# doc = self.format(record)
doc = {'record': self.format(record)}
self._write_doc(doc)
except (KeyboardInterrupt, SystemExit):
raise
except Exception:
self.handleError(record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment