Skip to content

Commit

Permalink
Fix key un-underscoring in json logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Valsecchi committed Aug 18, 2017
1 parent 4543684 commit 6471750
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ dist: .venv/timestamp
.venv/bin/python setup.py bdist_wheel

.PHONY: release
release: dist
release: acceptance dist
.venv/bin/twine upload dist/*.whl

run: build_test_app
Expand Down
19 changes: 10 additions & 9 deletions c2cwsgiutils/pyramid_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _un_underscore(message):
a lot of interesting fields with underscore. Therefore, it's a good idea to remove all those underscore
prefixes.
"""
for key, value in message.items():
for key, value in list(message.items()):
if key.startswith('_'):
new_key = key[1:]
if new_key not in message:
Expand All @@ -57,22 +57,18 @@ def _un_underscore(message):
return message


def new_make_message_dict(*args, **kargv):
def _make_message_dict(*args, **kargv):
"""
patch cee_syslog_handler to rename message->full_message otherwise this part is dropped by syslog.
"""
msg = orig_make_message_dict(*args, **kargv)
msg = cee_syslog_handler.make_message_dict(*args, **kargv)
if msg['message'] != msg['short_message']:
# only output full_message if it's different from short message
msg['full_message'] = msg['message']
del msg['message']
return _un_underscore(msg)


orig_make_message_dict = cee_syslog_handler.make_message_dict
cee_syslog_handler.make_message_dict = new_make_message_dict


class PyramidCeeSysLogHandler(cee_syslog_handler.CeeSysLogHandler):
"""
A CEE (JSON format) log handler with additional information about the current request.
Expand All @@ -81,6 +77,11 @@ def __init__(self, *args):
super().__init__(*args)
self.addFilter(_PYRAMID_FILTER)

def format(self, record):
message = _make_message_dict(record, self._debugging_fields, self._extra_fields, False, None,
self._facility)
return ": @cee: %s" % json.dumps(message)


class JsonLogHandler(logging.StreamHandler):
"""
Expand All @@ -91,8 +92,8 @@ def __init__(self, stream=None):
self.addFilter(_PYRAMID_FILTER)

def format(self, record):
message = cee_syslog_handler.make_message_dict(record, debugging_fields=True, extra_fields=True,
fqdn=False, localname=None, facility=None)
message = _make_message_dict(record, debugging_fields=True, extra_fields=True,
fqdn=False, localname=None, facility=None)
return json.dumps(message)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages


VERSION = '0.20.0'
VERSION = '0.20.1'
HERE = os.path.abspath(os.path.dirname(__file__))
INSTALL_REQUIRES = open(os.path.join(HERE, 'requirements.txt')).read().splitlines()

Expand Down

0 comments on commit 6471750

Please sign in to comment.