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

use go-logr context functions #1275

Closed
pohly opened this issue Nov 25, 2020 · 3 comments · Fixed by #1277
Closed

use go-logr context functions #1275

pohly opened this issue Nov 25, 2020 · 3 comments · Fixed by #1277
Assignees
Labels
good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.

Comments

@pohly
Copy link

pohly commented Nov 25, 2020

go-logr 0.3.0 added support for storing and retrieving a logger in a context: https://github.com/go-logr/logr/blob/4fa77cb7175ca349567983fd9cc9fd94462f41fa/logr.go#L197-L222

It would be good to implement

// FromContext returns a logger with predefined values from a context.Context.
func FromContext(ctx context.Context, keysAndValues ...interface{}) logr.Logger {
var log logr.Logger = Log
if ctx != nil {
if lv := ctx.Value(contextKey); lv != nil {
log = lv.(logr.Logger)
}
}
return log.WithValues(keysAndValues...)
}
// IntoContext takes a context and sets the logger as one of its keys.
// Use FromContext function to retrieve the logger.
func IntoContext(ctx context.Context, log logr.Logger) context.Context {
return context.WithValue(ctx, contextKey, log)
}
as wrapper around those functions because then code using the controller-runtime methods and code using go-logr directly become interoperable.

As it stands now, the loggers are different because the context keys are different.

@alvaroaleman
Copy link
Member

Sounds like a good idea
/good-first-issue

@k8s-ci-robot
Copy link
Contributor

@alvaroaleman:
This request has been marked as suitable for new contributors.

Please ensure the request meets the requirements listed here.

If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-good-first-issue command.

In response to this:

Sounds like a good idea
/good-first-issue

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. labels Nov 25, 2020
@arghya88
Copy link
Contributor

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants