From 4c972bc48b4759199989d42733a88fa4262b2338 Mon Sep 17 00:00:00 2001 From: Oleg Kainov Date: Wed, 11 Dec 2019 21:14:57 +0100 Subject: [PATCH] add IndexNameFrequency.NEVER Add possibility to write index to the exact name specified without any datetime suffixes. Fixes #69 --- README.rst | 4 ++-- cmreslogging/handlers.py | 17 ++++++++++++++--- tests/test_cmreshandler.py | 11 +++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index c2cff6b..5925536 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/cmreslogging/handlers.py b/cmreslogging/handlers.py index 52e250a..57c4048 100644 --- a/cmreslogging/handlers.py +++ b/cmreslogging/handlers.py @@ -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}] @@ -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, @@ -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 diff --git a/tests/test_cmreshandler.py b/tests/test_cmreshandler.py index d07dfdd..2e56bbe 100644 --- a/tests/test_cmreshandler.py +++ b/tests/test_cmreshandler.py @@ -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()