-
Notifications
You must be signed in to change notification settings - Fork 57
Closed
Labels
api: loggingIssues related to the googleapis/python-logging API.Issues related to the googleapis/python-logging API.priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Milestone
Description
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" }}')
- see
payload = encoded_msg.lstrip("{").rstrip("}") + "," - 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!
wrthwhl
Metadata
Metadata
Assignees
Labels
api: loggingIssues related to the googleapis/python-logging API.Issues related to the googleapis/python-logging API.priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.