Title: structured logging fails to create valid json in some cases · Issue #331 · googleapis/python-logging · GitHub
Open Graph Title: structured logging fails to create valid json in some cases · Issue #331 · googleapis/python-logging
X Title: structured logging fails to create valid json in some cases · Issue #331 · googleapis/python-logging
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...
Open Graph 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...
X 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...
Opengraph URL: https://github.com/googleapis/python-logging/issues/331
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"structured logging fails to create valid json in some cases","articleBody":"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.\r\n\r\n#### Environment details\r\n\r\n - code deployed on GCP Cloud Run\r\n - using library version from v3.0.0 branch\r\n \r\n#### Steps to reproduce\r\n\r\n1. use structured logging according to examples\r\n2. log any dict with double ending braces ( '{ \"x\": { \"y\" : \"z\" }}')\r\n3. see https://github.com/googleapis/python-logging/blob/3d03805ff9ab7cdf16c41f67a7453b2d8f5c70ce/google/cloud/logging_v2/handlers/structured_log.py#L69 using lstrip and rstrip, causing removal of both braces at the end\r\n4. receive textMessage instead of jsonPayload in GCP Logging, as invalid json was produced.\r\n\r\n#### Possible fix\r\n\r\nI am not a python programmer, so you might find better solutions, however, this is what worked for me:\r\n```\r\nif isinstance(record.msg, collections.abc.Mapping):\r\n encoded_msg = json.dumps(record.msg, ensure_ascii=False)\r\n payload_clean = encoded_msg.lstrip().rstrip() // remove whitespace\r\n # strip out open and close parentheses\r\n if payload_clean.startswith(\"{\"):\r\n payload_clean = payload_clean[1:] // strip leftmost character\r\n if payload_clean.endswith(\"}\"):\r\n payload_clean = payload_clean[:-1] // strip rightmost character\r\n payload = payload_clean + \",\"\r\n```\r\n\r\nThanks a lot for bringing structured (json) logging into the lib!","author":{"url":"https://github.com/EricLacherSolera","@type":"Person","name":"EricLacherSolera"},"datePublished":"2021-06-21T11:03:16.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/331/python-logging/issues/331"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bc4e5fa3-ec67-3620-f6fd-7426390e66fb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B804:34DA5A:3DB5548:578EF30:6A4FB10D |
| html-safe-nonce | 0b58ae6cf33624cd59b17f5ba06369c4b651e8c1014b474f5b19743c9178e285 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODA0OjM0REE1QTozREI1NTQ4OjU3OEVGMzA6NkE0RkIxMEQiLCJ2aXNpdG9yX2lkIjoiNjIxNzA1NzkxNjg1NzMzMTk4MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 60eaaed9359d0175ef743e1636368725ffd82ae95be4d2f42c0dd7e9aabc3771 |
| hovercard-subject-tag | issue:926097751 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/python-logging/331/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7228721f3e18f88f1e649b52a52e657debdf9b378736d02ae89f264f0255c926/googleapis/python-logging/issues/331 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7228721f3e18f88f1e649b52a52e657debdf9b378736d02ae89f264f0255c926/googleapis/python-logging/issues/331 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EricLacherSolera |
| hostname | github.com |
| expected-hostname | github.com |
| None | b92d11c0aa4a77d54ef4af1078b6a15fb5a70a215b30c4ecf28889d5a8e656d9 |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/python-logging git https://github.com/googleapis/python-logging.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 226992562 |
| octolytics-dimension-repository_nwo | googleapis/python-logging |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 226992562 |
| octolytics-dimension-repository_network_root_nwo | googleapis/python-logging |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4b249b445842943ed31549e027f57a8ade9881ed |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width