Created
August 21, 2018 23:06
-
-
Save finomayato/fd7cf6be49aaed39e947869eb6e06c21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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