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

Minor logger improovments #1495

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ type (
WithSkipCallers interface {
WithCallerSkip(int) Logger
}

// WithLogger is an optional interface that prepend every log entry with keyvals.
// This call must not mutate the original logger.
WithLogger interface {
With(keyvals ...interface{}) Logger
}
)
7 changes: 1 addition & 6 deletions log/with_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@

package log

// WithLogger is an interface that prepend every log entry with keyvals.
type WithLogger interface {
With(keyvals ...interface{}) Logger
}

// With creates a child Logger that includes the supplied key-value pairs in each log entry. It does this by
// using the supplied logger if it implements WithLogger; otherwise, it does so by intercepting every log call.
func With(logger Logger, keyvals ...interface{}) Logger {
Expand Down Expand Up @@ -57,7 +52,7 @@ type withLogger struct {
}

func newWithLogger(logger Logger, keyvals ...interface{}) *withLogger {
return &withLogger{logger: logger, keyvals: keyvals}
return &withLogger{logger: Skip(logger, 1), keyvals: keyvals}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, this code path would only matter if the logger did not implement WithLogger but did implement WithSkipCallers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you right.
Skip function will ignore logger in case it doesn't implement WithSkipCallers interface

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm I wonder how common this actually is, but in principle this change makes sense I think

}

func (l *withLogger) prependKeyvals(keyvals []interface{}) []interface{} {
Expand Down
Loading