From ac99f6c64c5e1404e52b2603ee422d48ae244751 Mon Sep 17 00:00:00 2001 From: nir0s Date: Tue, 13 Mar 2018 12:34:53 +0200 Subject: [PATCH] Return consolidate context dict instead of list --- tests/test_wryte.py | 7 ++----- wryte.py | 40 ++++++++++++---------------------------- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/tests/test_wryte.py b/tests/test_wryte.py index 2f14342..1187507 100644 --- a/tests/test_wryte.py +++ b/tests/test_wryte.py @@ -60,7 +60,6 @@ def test_context_type(self): {'k3': 'v3', 'k4': 'v4'}, {'k7': 'v7', 'k8': {'k9': 'v9'}}, '{"k5": "v5", "k6": "v6"}', - 'k10=v10', 'bla', k1='v1', k2='v2', ) @@ -89,16 +88,14 @@ def test_event(self): def test_bind_unbind(self): w = Wryte(name=str(uuid.uuid4())) assert 'k' not in w._log.keys() - w.bind({'k1': 'v1'}, 'k2=v2', '{"k3": "v3"}', k4='v4') + w.bind({'k1': 'v1'}, '{"k2": "v2"}', k3='v3') assert 'k1' in w._log.keys() assert 'k2' in w._log.keys() assert 'k3' in w._log.keys() - assert 'k4' in w._log.keys() - w.unbind('k1', 'k2', 'k3', 'k4') + w.unbind('k1', 'k2', 'k3') assert 'k1' not in w._log.keys() assert 'k2' not in w._log.keys() assert 'k3' not in w._log.keys() - assert 'k4' not in w._log.keys() def test_bare_handler(self): w = Wryte(name=str(uuid.uuid4()), bare=True) diff --git a/wryte.py b/wryte.py index e4776b9..9cf33a5 100644 --- a/wryte.py +++ b/wryte.py @@ -209,15 +209,6 @@ def _get_base(name, hostname): 'type': 'log' } - @staticmethod - def _split_kv(pair): - """Return dict for key=value. - """ - # TODO: Document that this is costly. - # TODO: Document that it's only split once. - kv = pair.split('=', 1) - return {kv[0]: kv[1]} - @staticmethod def _get_timestamp(): # TODO: Allow to use udatetime instead for faster evals @@ -235,22 +226,19 @@ def _normalize_objects(self, objects): doesn't fit the supported formats. """ # TODO: Generate a consolidated dict instead of a list of objects - normalized_objects = [] + consolidated = {} + for obj in objects: try: - if isinstance(obj, dict): - normalized_objects.append(obj) - else: - normalized_objects.append(json.loads(obj)) - # TODO: Should be a JsonDecoderError - except Exception: # NOQA - if '=' in obj: - # TODO: Remove supports for kv pair strings - normalized_objects.append(self._split_kv(obj)) - else: - normalized_objects.append( + consolidated.update(obj) + except ValueError: + try: + consolidated.update(json.loads(obj)) + # # TODO: Should be a JsonDecoderError + except Exception: # NOQA + consolidated.update( {'_bad_object_{0}'.format(str(uuid.uuid4())): obj}) - return normalized_objects + return consolidated def _enrich(self, message, level, objects, kwargs=None): """Return a metadata enriched object which includes the level, @@ -276,9 +264,7 @@ def _enrich(self, message, level, objects, kwargs=None): log = self._log.copy() # Normalizes and adds dictionary-like context. - objects = self._normalize_objects(objects) - for part in objects: - log.update(part) + log.update(self._normalize_objects(objects)) # Adds k=v like context if kwargs: @@ -561,9 +547,7 @@ def bind(self, *objects, **kwargs): After binding, each log entry will contain the bound fields. """ - objects = self._normalize_objects(objects) - for part in objects: - self._log.update(part) + self._log.update(self._normalize_objects(objects)) if kwargs: self._log.update(kwargs)