Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StructuredLogHandler adapter #750

Open
NewGlad opened this issue May 25, 2023 · 0 comments · May be fixed by #751
Open

Add StructuredLogHandler adapter #750

NewGlad opened this issue May 25, 2023 · 0 comments · May be fixed by #751
Labels
api: logging Issues related to the googleapis/python-logging API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@NewGlad
Copy link

NewGlad commented May 25, 2023

Thanks for stopping by to let us know something could be better!

Is your feature request related to a problem? Please describe.

In my project hosted in Google Cloud, I use StructuredLogHandler and quite often I need to pass some predefined context to log labels for easier system monitoring.
Let's say it could be some service configuration values, code versions, and many more.

Basically, I need to pass such context data every logger call: logger.info('my message', extra={'labels': ...}} which is hard to maintain.

Describe the solution you'd like

For this case, we have logging.LoggerAdapter in the Python standard library.
And I propose adding a specific adapter to support the Google Cloud format for this purpose.
I have implemented it already in my project, and think it could be useful for everyone.
With that adapter, I need to configure my logging context once and use my logger in an easier manner.
Let's look at the example:

import logging
from google.cloud.logging_v2.handlers.structured_log import AppendLabelLoggingAdapter
from google.cloud.logging_v2.handlers.structured_log import StructuredLogHandler
logging.root.setLevel(logging.INFO)
logging.root.handlers = [StructuredLogHandler()]
first_adapter = AppendLabelLoggingAdapter(logging.root, {'a': 5, 'b': 6})
first_adapter.info('first info')
{
    "message": "first info",
    "severity": "INFO",
    "logging.googleapis.com/labels": {"python_logger": "root", "a": 5, "b": 6}
    [...]
}
# Could be stacked
second_adapter=AppendLabelLoggingAdapter(first_adapter, {'hello': 'world'})
second_adapter.info('second info')
{
    "message": "second info",
    "severity": "INFO",
    "logging.googleapis.com/labels": {"python_logger": "root", "hello": "world", "a": 5, "b": 6}
    [...]
}
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/python-logging API. label May 25, 2023
@NewGlad NewGlad linked a pull request May 25, 2023 that will close this issue
4 tasks
@losalex losalex removed their assignment Jul 3, 2023
@gkevinzheng gkevinzheng added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p3 Desirable enhancement or fix. May not be included in next release. labels Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/python-logging API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants