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

Logging: detect log level through layers of wrappers #10646

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

Conversation

bboreham
Copy link
Contributor

@bboreham bboreham commented Feb 14, 2025

What this PR does

Expose log level through log wrappers, so that Debug() calls are fast when debug logging is disabled.

It works by making a Log() call so it reaches the bottom layer.

However, if SlogFromGoKit is used with a logger outside of this package, you will get a spurious log line.

Which issue(s) this PR fixes or relates to

Inspired by prometheus/prometheus#15993

This is a competitor to #10604. Credit @dimitarvdimitrov for the idea.

Before:

BenchmarkSlogFromGoKit/log-28             981555              1176 ns/op            1008 B/op         16 allocs/op
BenchmarkSlogFromGoKit/debuglog-28        949378              1175 ns/op            1008 B/op         16 allocs/op
BenchmarkDebugLog-28                    26430567                44.55 ns/op           96 B/op          2 allocs/op

After:

BenchmarkSlogFromGoKit/log-28             994729              1165 ns/op            1008 B/op         16 allocs/op
BenchmarkSlogFromGoKit/debuglog-28      358293070                3.342 ns/op           0 B/op          0 allocs/op
BenchmarkDebugLog-28                    24998392                45.67 ns/op           96 B/op          2 allocs/op

Checklist

  • Tests updated.
  • Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • about-versioning.md updated with experimental features.

Checking the difference between Info and Debug logs.

Signed-off-by: Bryan Boreham <[email protected]>
Make a Log() call so it reaches the bottom layer.

If SlogFromGoKit is used with a logger outside of this package, you will get a spurious log line.

Signed-off-by: Bryan Boreham <[email protected]>
@bboreham bboreham requested a review from a team as a code owner February 14, 2025 11:48
Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

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

LGTM, it's how I imagined it. But would be nice to get a second/third opinion

// If we are called with a special magic struct, use it to pass back the logging level.
func (f levelFilter) Log(keyvals ...interface{}) error {
if len(keyvals) > 0 {
if x, ok := keyvals[len(keyvals)-1].(*privateLevelDetector); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

i'm shocked the type assertion doesn't make it significantly slower

@dimitarvdimitrov
Copy link
Contributor

for the record: I prefer this approach because the other one requires committers be aware of and keep the conventions in the package (of using wrap in #10604)

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