Skip to content
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
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

Closed

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!

Activity

  1. added
    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.
    on Jun 23, 2021
  2. added this to the v3.0.0 Breaking milestone on Jun 23, 2021
  3. daniel-sanche commented on Jun 24, 2021

    @daniel-sanche
    Contributor

    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!

  4. linked a pull request that will close this issuefeat!: support string-encoded json #339on Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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