You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
structured logging fails to create valid json in some cases #331
I think I found a bug in v3.0.0 branch, causing invalid json being generated, thus having no jsonPayload in GCP Logging and instead receiving a textMessage.
Environment details
code deployed on GCP Cloud Run
using library version from v3.0.0 branch
Steps to reproduce
use structured logging according to examples
log any dict with double ending braces ( '{ "x": { "y" : "z" }}')
using lstrip and rstrip, causing removal of both braces at the end
receive textMessage instead of jsonPayload in GCP Logging, as invalid json was produced.
Possible fix
I am not a python programmer, so you might find better solutions, however, this is what worked for me:
if isinstance(record.msg, collections.abc.Mapping):
encoded_msg = json.dumps(record.msg, ensure_ascii=False)
payload_clean = encoded_msg.lstrip().rstrip() // remove whitespace
# strip out open and close parentheses
if payload_clean.startswith("{"):
payload_clean = payload_clean[1:] // strip leftmost character
if payload_clean.endswith("}"):
payload_clean = payload_clean[:-1] // strip rightmost character
payload = payload_clean + ","
Thanks a lot for bringing structured (json) logging into the lib!
Activity
daniel-sanche commentedon Jun 24, 2021
Thanks for bringing this up, I added support for string-encoded dictionaries in #339
FYI, the v3.0.0 branch is a WIP so may not always be fully bug-free before release, but I appreciate the early feedback!