Skip to content

Commit

Permalink
Return consolidate context dict instead of list
Browse files Browse the repository at this point in the history
  • Loading branch information
nir0s committed Mar 13, 2018
1 parent 2add2dc commit ac99f6c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
7 changes: 2 additions & 5 deletions tests/test_wryte.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 12 additions & 28 deletions wryte.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ac99f6c

Please sign in to comment.