Skip to content

Custome ES records names #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions cmreslogging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class IndexNameFrequency(Enum):
__DEFAULT_ES_DOC_TYPE = 'python_log'
__DEFAULT_RAISE_ON_EXCEPTION = False
__DEFAULT_TIMESTAMP_FIELD_NAME = "timestamp"
__DEFAULT_ES_RECORDS_MAPPING = {}

__LOGGING_FILTER_FIELDS = ['msecs',
'relativeCreated',
Expand Down Expand Up @@ -138,7 +139,8 @@ def __init__(self,
es_doc_type=__DEFAULT_ES_DOC_TYPE,
es_additional_fields=__DEFAULT_ADDITIONAL_FIELDS,
raise_on_indexing_exceptions=__DEFAULT_RAISE_ON_EXCEPTION,
default_timestamp_field_name=__DEFAULT_TIMESTAMP_FIELD_NAME):
default_timestamp_field_name=__DEFAULT_TIMESTAMP_FIELD_NAME,
es_records_name_mapping_dict=__DEFAULT_ES_RECORDS_MAPPING):
""" Handler constructor

:param hosts: The list of hosts that elasticsearch clients will connect. The list can be provided
Expand Down Expand Up @@ -172,6 +174,8 @@ def __init__(self,
to the logs, such the application, environment, etc.
:param raise_on_indexing_exceptions: A boolean, True only for debugging purposes to raise exceptions
caused when
:param es_records_name_mapping_dict: A dictionary with records name mappings, as key is expected the old name and
as value the wanted name in ES. {'old_name':'new_name'}
:return: A ready to be used CMRESHandler.
"""
logging.Handler.__init__(self)
Expand All @@ -194,6 +198,7 @@ def __init__(self,
'host_ip': socket.gethostbyname(socket.gethostname())})
self.raise_on_indexing_exceptions = raise_on_indexing_exceptions
self.default_timestamp_field_name = default_timestamp_field_name
self.es_records_name_mapping_dict = es_records_name_mapping_dict

self._client = None
self._buffer = []
Expand Down Expand Up @@ -328,9 +333,10 @@ def emit(self, record):
rec = self.es_additional_fields.copy()
for key, value in record.__dict__.items():
if key not in CMRESHandler.__LOGGING_FILTER_FIELDS:
if key == "args":
value = tuple(str(arg) for arg in value)
rec[key] = "" if value is None else value
if key in self.es_records_name_mapping_dict:
rec[self.es_records_name_mapping_dict[key]] = "" if value is None else value
else:
rec[key] = "" if value is None else value
rec[self.default_timestamp_field_name] = self.__get_es_datetime_str(record.created)
with self._buffer_lock:
self._buffer.append(rec)
Expand Down