René's URL Explorer Experiment


Title: Add support to custom `JSONEncoder` · Issue #656 · googleapis/python-logging · GitHub

Open Graph Title: Add support to custom `JSONEncoder` · Issue #656 · googleapis/python-logging

X Title: Add support to custom `JSONEncoder` · Issue #656 · googleapis/python-logging

Description: Is your feature request related to a problem? Please describe. My company is moving out from structlog to start using this package, as it is intended to have better integration with google products. We're using StructuredLogHandler to se...

Open Graph Description: Is your feature request related to a problem? Please describe. My company is moving out from structlog to start using this package, as it is intended to have better integration with google products...

X Description: Is your feature request related to a problem? Please describe. My company is moving out from structlog to start using this package, as it is intended to have better integration with google products...

Opengraph URL: https://github.com/googleapis/python-logging/issues/656

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add support to custom `JSONEncoder`","articleBody":" **Is your feature request related to a problem? Please describe.**\r\nMy company is moving out from [structlog](https://www.structlog.org/en/stable/) to start using this package, as it is intended to have better integration with google products. We're using `StructuredLogHandler` to send the log records to google cloud logging. But something structlog used to do out-of-the-box was the ability to automatically convert the extra data we provide to the logger to be sent to the stream, and therefore, we need to check all the calls we're making to `logger.info()`, `debug()`, `warning()`, etc. to check if the data type is supported by JSONEncoder, otherwise the call to `hadler.format()` will raise `TypeError`, stating that `Object of type PosixPath is not JSON serializable` (this is one example of error we're getting, because the default encoder cannot encode `pathlib.Path` instances) and the log record will not be pushed to gcloud logging\r\n **Describe the solution you'd like**\r\nI'd like to be able to specify a custom class to encode the message to JSON, so the formatting is handled by the encoder, not the caller of `logger.info()` and its siblings\r\n **Describe alternatives you've considered**\r\nAn alternative would be having a filter that prepares the data to be used by the handler, I'm even wrote a filter to handle that:\r\n```python\r\nclass JsonableConverterFilter(logging.Filter):\r\n    \"\"\"Prepare log record to be JSON encoded\r\n\r\n    :class:`google.cloud.logging_v2.handlers.StructuredLogHandler` encodes directly to default JSON encoder without\r\n    the possibility to set up a custom encoder, so we need to hydrate the log record to not break when emitting\r\n    (because broken logs break silently)\r\n\r\n    The ideal solution would be to have a LogFormatter, but as GCloud's log handler formats to JSON inside the handler,\r\n    we need to circumvent this issue with a :class:`logging.Filter`\r\n    \"\"\"\r\n\r\n    def filter(self, record: logging.LogRecord) -\u003e bool:\r\n        fields: Dict[str, Any] = getattr(record, \"json_fields\", {})\r\n\r\n        for key, value in fields.items():\r\n            fields[key] = JsonableConverterFilter.prepare_field(value)\r\n\r\n        return True\r\n\r\n    @classmethod\r\n    def prepare_field(cls, value: Any) -\u003e TypeJSON:\r\n        \"\"\"Converts the provided type into a plain JSON type\"\"\"\r\n        # basic types\r\n        if isinstance(value, (int, str, float, bool)) or value is None:\r\n            return value\r\n\r\n        try:\r\n            iterator = iter(value)\r\n        except TypeError:\r\n            # probably a custom class\r\n            if not hasattr(value, \"__dict__\"):\r\n                return str(value)\r\n\r\n            iterator = iter(value.__dict__)\r\n\r\n        # hashmaps\r\n        if hasattr(value, \"keys\") and hasattr(value, \"values\"):\r\n            return {key: cls.prepare_field(val) for key, val in value.items()}\r\n\r\n        # sequences\r\n        return [cls.prepare_field(item) for item in iterator]\r\n\r\n```\r\nbut it seems to me I'm deviating the purpose of log filters by doing that\r\n **Additional context**\r\nn/a for now\r\n","author":{"url":"https://github.com/thiromi","@type":"Person","name":"thiromi"},"datePublished":"2022-10-25T11:03:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/656/python-logging/issues/656"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:08a3cc49-859a-de5d-2d21-a6470cafb3eb
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id928E:34DA5A:26EB46D:363AE5E:6A4F57E5
html-safe-nonce492add88342b8fc0de145f411ac2da97b1da1895b58b5d4af12dd34be4e73247
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjhFOjM0REE1QToyNkVCNDZEOjM2M0FFNUU6NkE0RjU3RTUiLCJ2aXNpdG9yX2lkIjoiNjIwNjY0NTg3MjQ1NDc1MjIyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacab5049566baa9b4d183c0dac881af78e6f7139286ac5e6cb16c86fe6c2a123b1
hovercard-subject-tagissue:1422279865
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/python-logging/656/issue_layout
twitter:imagehttps://opengraph.githubassets.com/5f7872a543f4a5d9fc147f09b35306b3fe5e1a4a1ac47e6f3187e81b123f6770/googleapis/python-logging/issues/656
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/5f7872a543f4a5d9fc147f09b35306b3fe5e1a4a1ac47e6f3187e81b123f6770/googleapis/python-logging/issues/656
og:image:altIs your feature request related to a problem? Please describe. My company is moving out from structlog to start using this package, as it is intended to have better integration with google products...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamethiromi
hostnamegithub.com
expected-hostnamegithub.com
Noneb92d11c0aa4a77d54ef4af1078b6a15fb5a70a215b30c4ecf28889d5a8e656d9
turbo-cache-controlno-preview
go-importgithub.com/googleapis/python-logging git https://github.com/googleapis/python-logging.git
octolytics-dimension-user_id16785467
octolytics-dimension-user_logingoogleapis
octolytics-dimension-repository_id226992562
octolytics-dimension-repository_nwogoogleapis/python-logging
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id226992562
octolytics-dimension-repository_network_root_nwogoogleapis/python-logging
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseb14b8c6d72fdb96ca48aba85dec94fe96ce5adbe
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/googleapis/python-logging/issues/656#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fpython-logging%2Fissues%2F656
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fpython-logging%2Fissues%2F656
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=googleapis%2Fpython-logging
Reloadhttps://github.com/googleapis/python-logging/issues/656
Reloadhttps://github.com/googleapis/python-logging/issues/656
Reloadhttps://github.com/googleapis/python-logging/issues/656
Please reload this pagehttps://github.com/googleapis/python-logging/issues/656
googleapis https://github.com/googleapis
python-logginghttps://github.com/googleapis/python-logging
Notifications https://github.com/login?return_to=%2Fgoogleapis%2Fpython-logging
Fork 59 https://github.com/login?return_to=%2Fgoogleapis%2Fpython-logging
Star 142 https://github.com/login?return_to=%2Fgoogleapis%2Fpython-logging
Code https://github.com/googleapis/python-logging
Issues 0 https://github.com/googleapis/python-logging/issues
Pull requests 0 https://github.com/googleapis/python-logging/pulls
Actions https://github.com/googleapis/python-logging/actions
Projects https://github.com/googleapis/python-logging/projects
Security and quality 0 https://github.com/googleapis/python-logging/security
Insights https://github.com/googleapis/python-logging/pulse
Code https://github.com/googleapis/python-logging
Issues https://github.com/googleapis/python-logging/issues
Pull requests https://github.com/googleapis/python-logging/pulls
Actions https://github.com/googleapis/python-logging/actions
Projects https://github.com/googleapis/python-logging/projects
Security and quality https://github.com/googleapis/python-logging/security
Insights https://github.com/googleapis/python-logging/pulse
#657https://github.com/googleapis/python-logging/pull/657
Add support to custom JSONEncoderhttps://github.com/googleapis/python-logging/issues/656#top
#657https://github.com/googleapis/python-logging/pull/657
https://github.com/daniel-sanche
api: loggingIssues related to the googleapis/python-logging API.https://github.com/googleapis/python-logging/issues?q=state%3Aopen%20label%3A%22api%3A%20logging%22
priority: p3Desirable enhancement or fix. May not be included in next release.https://github.com/googleapis/python-logging/issues?q=state%3Aopen%20label%3A%22priority%3A%20p3%22
type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.https://github.com/googleapis/python-logging/issues?q=state%3Aopen%20label%3A%22type%3A%20feature%20request%22
https://github.com/thiromi
thiromihttps://github.com/thiromi
on Oct 25, 2022https://github.com/googleapis/python-logging/issues/656#issue-1422279865
structloghttps://www.structlog.org/en/stable/
daniel-sanchehttps://github.com/daniel-sanche
api: loggingIssues related to the googleapis/python-logging API.https://github.com/googleapis/python-logging/issues?q=state%3Aopen%20label%3A%22api%3A%20logging%22
priority: p3Desirable enhancement or fix. May not be included in next release.https://github.com/googleapis/python-logging/issues?q=state%3Aopen%20label%3A%22priority%3A%20p3%22
type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.https://github.com/googleapis/python-logging/issues?q=state%3Aopen%20label%3A%22type%3A%20feature%20request%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.