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

added the ability to attach fields to Agent logger #1309

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

davidzhao
Copy link
Member

also logging worker_id to improve debugging

also logging worker_id to improve debugging
@davidzhao davidzhao requested a review from a team December 29, 2024 05:58
Copy link

changeset-bot bot commented Dec 29, 2024

🦋 Changeset detected

Latest commit: 9380596

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
livekit-agents Patch
livekit-plugins-openai Patch

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

Comment on lines +8 to +24
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})
Copy link
Member

@theomonnom theomonnom Dec 29, 2024

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

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants