-
Notifications
You must be signed in to change notification settings - Fork 561
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
added the ability to attach fields to Agent logger #1309
base: main
Are you sure you want to change the base?
Conversation
also logging worker_id to improve debugging
🦋 Changeset detectedLatest commit: 9380596 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
class FieldsLogger(logging.LoggerAdapter): | ||
"""A logger adapter that adds fields to each log message""" | ||
|
||
def __init__(self, logger: logging.Logger, fields: dict[str, Any] = {}) -> None: | ||
super().__init__(logger, fields) | ||
|
||
def process(self, msg, kwargs): | ||
# Merge global fields with any per-message extras | ||
extras = self.extra | ||
if "extra" in kwargs: | ||
extras = self.extra.copy() | ||
extras.update(kwargs["extra"]) | ||
kwargs["extra"] = extras | ||
return msg, kwargs | ||
|
||
def with_fields(self, fields: dict[str, Any]) -> "FieldsLogger": | ||
return FieldsLogger(self.logger, {**self.extra, **fields}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed if we allow people to use their own loggers.
The relevant thing to do would be to create an example that use a custom logger for everything (even plugins).
People may want to inject fields for the whole process, not only what's inside the agent class (since it is internal anyway).
The right way to do it would be to provide a callback in the cli/worker initialization that allow people to configure their own loggers (and also "extend" the default one). This way we will be directly flexible. I feel like just exposing fields is only one usecase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that I agree.. the loggers in python are pretty primitive. this is just porting over the pattern that has worked well for us on the Go side here.
if folks are using their own loggers, there still isn't a simple way to attach fields to it so that they are logged each time.. even our own implementation of adding pid feels hacky.
also logging worker_id to improve debugging