Skip to content

Commit 4c972bc

Browse files
committed
add IndexNameFrequency.NEVER
Add possibility to write index to the exact name specified without any datetime suffixes. Fixes cmanaha#69
1 parent e4a16a8 commit 4c972bc

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ The constructors takes the following parameters:
109109
- es_index_name: A string with the prefix of the elasticsearch index that will be created. Note a date with
110110
YYYY.MM.dd, ``python_logger`` used by default
111111
- index_name_frequency: The frequency to use as part of the index naming. Currently supports
112-
CMRESHandler.IndexNameFrequency.DAILY, CMRESHandler.IndexNameFrequency.WEEKLY,
113-
CMRESHandler.IndexNameFrequency.MONTHLY, CMRESHandler.IndexNameFrequency.YEARLY by default the daily rotation
112+
``CMRESHandler.IndexNameFrequency.DAILY``, ``CMRESHandler.IndexNameFrequency.WEEKLY``,
113+
``CMRESHandler.IndexNameFrequency.MONTHLY``, ``CMRESHandler.IndexNameFrequency.YEARLY`` and ``CMRESHandler.IndexNameFrequency.NEVER``. By default the daily rotation
114114
is used
115115
- es_doc_type: A string with the name of the document type that will be used ``python_log`` used by default
116116
- es_additional_fields: A dictionary with all the additional fields that you would like to add to the logs

cmreslogging/handlers.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ class IndexNameFrequency(Enum):
5151
- Weekly indices
5252
- Monthly indices
5353
- Year indices
54+
- Never expiring indices
5455
"""
5556
DAILY = 0
5657
WEEKLY = 1
5758
MONTHLY = 2
5859
YEARLY = 3
60+
NEVER = 4
5961

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

120+
@staticmethod
121+
def _get_never_index_name(es_index_name):
122+
""" Return elasticsearch index name
123+
:param: index_name the prefix to be used in the index
124+
:return: A srting containing the elasticsearch indexname used which should include just the index name
125+
"""
126+
return "{0!s}".format(es_index_name)
127+
118128
_INDEX_FREQUENCY_FUNCION_DICT = {
119129
IndexNameFrequency.DAILY: _get_daily_index_name,
120130
IndexNameFrequency.WEEKLY: _get_weekly_index_name,
121131
IndexNameFrequency.MONTHLY: _get_monthly_index_name,
122-
IndexNameFrequency.YEARLY: _get_yearly_index_name
132+
IndexNameFrequency.YEARLY: _get_yearly_index_name,
133+
IndexNameFrequency.NEVER: _get_never_index_name,
123134
}
124135

125136
def __init__(self,
@@ -164,8 +175,8 @@ def __init__(self,
164175
date with YYYY.MM.dd, ```python_logger``` used by default
165176
:param index_name_frequency: Defines what the date used in the postfix of the name would be. available values
166177
are selected from the IndexNameFrequency class (IndexNameFrequency.DAILY,
167-
IndexNameFrequency.WEEKLY, IndexNameFrequency.MONTHLY, IndexNameFrequency.YEARLY). By default
168-
it uses daily indices.
178+
IndexNameFrequency.WEEKLY, IndexNameFrequency.MONTHLY, IndexNameFrequency.YEARLY,
179+
IndexNameFrequency.NEVER). By default it uses daily indices.
169180
:param es_doc_type: A string with the name of the document type that will be used ```python_log``` used
170181
by default
171182
:param es_additional_fields: A dictionary with all the additional fields that you would like to add

tests/test_cmreshandler.py

+11
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ def test_index_name_frequency_functions(self):
169169
CMRESHandler._get_yearly_index_name(index_name)
170170
)
171171

172+
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
173+
auth_type=CMRESHandler.AuthType.NO_AUTH,
174+
es_index_name=index_name,
175+
use_ssl=False,
176+
index_name_frequency=CMRESHandler.IndexNameFrequency.NEVER,
177+
raise_on_indexing_exceptions=True)
178+
self.assertEqual(
179+
handler._index_name_func.__func__(index_name),
180+
CMRESHandler._get_never_index_name(index_name)
181+
)
182+
172183

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

0 commit comments

Comments
 (0)