Skip to content

structured logging fails to create valid json in some cases #331

@EricLacherSolera

Description

@EricLacherSolera

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

  1. use structured logging according to examples
  2. log any dict with double ending braces ( '{ "x": { "y" : "z" }}')
  3. see
    payload = encoded_msg.lstrip("{").rstrip("}") + ","
    using lstrip and rstrip, causing removal of both braces at the end
  4. 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!

Metadata

Metadata

Assignees

Labels

api: loggingIssues related to the googleapis/python-logging API.priority: p2Moderately-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.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions