Skip to content

Commit

Permalink
add IndexNameFrequency.NEVER
Browse files Browse the repository at this point in the history
Add possibility to write index to the exact name specified without
any datetime suffixes.

Fixes #69
  • Loading branch information
okainov committed Dec 11, 2019
1 parent e4a16a8 commit 4c972bc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ The constructors takes the following parameters:
- es_index_name: A string with the prefix of the elasticsearch index that will be created. Note a date with
YYYY.MM.dd, ``python_logger`` used by default
- index_name_frequency: The frequency to use as part of the index naming. Currently supports
CMRESHandler.IndexNameFrequency.DAILY, CMRESHandler.IndexNameFrequency.WEEKLY,
CMRESHandler.IndexNameFrequency.MONTHLY, CMRESHandler.IndexNameFrequency.YEARLY by default the daily rotation
``CMRESHandler.IndexNameFrequency.DAILY``, ``CMRESHandler.IndexNameFrequency.WEEKLY``,
``CMRESHandler.IndexNameFrequency.MONTHLY``, ``CMRESHandler.IndexNameFrequency.YEARLY`` and ``CMRESHandler.IndexNameFrequency.NEVER``. By default the daily rotation
is used
- es_doc_type: A string with the name of the document type that will be used ``python_log`` used by default
- es_additional_fields: A dictionary with all the additional fields that you would like to add to the logs
Expand Down
17 changes: 14 additions & 3 deletions cmreslogging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ class IndexNameFrequency(Enum):
- Weekly indices
- Monthly indices
- Year indices
- Never expiring indices
"""
DAILY = 0
WEEKLY = 1
MONTHLY = 2
YEARLY = 3
NEVER = 4

# Defaults for the class
__DEFAULT_ELASTICSEARCH_HOST = [{'host': 'localhost', 'port': 9200}]
Expand Down Expand Up @@ -115,11 +117,20 @@ def _get_yearly_index_name(es_index_name):
"""
return "{0!s}-{1!s}".format(es_index_name, datetime.datetime.now().strftime('%Y'))

@staticmethod
def _get_never_index_name(es_index_name):
""" Return elasticsearch index name
:param: index_name the prefix to be used in the index
:return: A srting containing the elasticsearch indexname used which should include just the index name
"""
return "{0!s}".format(es_index_name)

_INDEX_FREQUENCY_FUNCION_DICT = {
IndexNameFrequency.DAILY: _get_daily_index_name,
IndexNameFrequency.WEEKLY: _get_weekly_index_name,
IndexNameFrequency.MONTHLY: _get_monthly_index_name,
IndexNameFrequency.YEARLY: _get_yearly_index_name
IndexNameFrequency.YEARLY: _get_yearly_index_name,
IndexNameFrequency.NEVER: _get_never_index_name,
}

def __init__(self,
Expand Down Expand Up @@ -164,8 +175,8 @@ def __init__(self,
date with YYYY.MM.dd, ```python_logger``` used by default
:param index_name_frequency: Defines what the date used in the postfix of the name would be. available values
are selected from the IndexNameFrequency class (IndexNameFrequency.DAILY,
IndexNameFrequency.WEEKLY, IndexNameFrequency.MONTHLY, IndexNameFrequency.YEARLY). By default
it uses daily indices.
IndexNameFrequency.WEEKLY, IndexNameFrequency.MONTHLY, IndexNameFrequency.YEARLY,
IndexNameFrequency.NEVER). By default it uses daily indices.
:param es_doc_type: A string with the name of the document type that will be used ```python_log``` used
by default
:param es_additional_fields: A dictionary with all the additional fields that you would like to add
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cmreshandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ def test_index_name_frequency_functions(self):
CMRESHandler._get_yearly_index_name(index_name)
)

handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
auth_type=CMRESHandler.AuthType.NO_AUTH,
es_index_name=index_name,
use_ssl=False,
index_name_frequency=CMRESHandler.IndexNameFrequency.NEVER,
raise_on_indexing_exceptions=True)
self.assertEqual(
handler._index_name_func.__func__(index_name),
CMRESHandler._get_never_index_name(index_name)
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4c972bc

Please sign in to comment.