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

Enabled + stack unwinding #215

Closed
pohly opened this issue Aug 30, 2023 · 7 comments · Fixed by #218
Closed

Enabled + stack unwinding #215

pohly opened this issue Aug 30, 2023 · 7 comments · Fixed by #218
Assignees

Comments

@pohly
Copy link
Contributor

pohly commented Aug 30, 2023

RuntimeInfo.CallDepth is documented as:

	// CallDepth is the number of call frames the logr library adds between the
	// end-user and the LogSink.  LogSink implementations which choose to print
	// the original logging site (e.g. file & line) should climb this many
	// additional frames to find it.

It's 1, which is correct for the LogSink.Info and LogSink.Error calls, e.g.

logr/logr.go

Line 278 in eea184c

l.sink.Info(l.level, msg, keysAndValues...)

But it is not correct for the LogSink.Enabled call in Logger.Info:

logr/logr.go

Line 274 in eea184c

if l.Enabled() {

There the call chain is <user code> -> Logger.Info -> Logger.Enabled -> LogSink.Enabled.

It is correct again for <user code> -> Logger.Enabled -> LogSink.Enabled.

@pohly
Copy link
Contributor Author

pohly commented Aug 30, 2023

My initial reaction was that Logger.Info needs to be changed, but existing code is more likely to have tests for that code path and thus might have been written so that it compensates for the bug. Fixing the bug will break that code.

I'll check with klog, which does stack unwinding in Enabled for -vmodule.

I noticed because I am using the if loggerV := logger.V(6); loggerV.Enabled() pattern and didn't get the expected output when combining that with -vmodule.

@pohly
Copy link
Contributor Author

pohly commented Aug 30, 2023

I did indeed adapt to this in kubernetes/klog@125ecfe.

The Enabled methods skip one additional stack frame to match the behavior of Logger.Info. What I hadn't realized at that time is that they also get called through Logger.Enabled directly by the user code.

@thockin
Copy link
Contributor

thockin commented Sep 1, 2023

Why does Enabled() care about call depth?

@pohly
Copy link
Contributor Author

pohly commented Sep 2, 2023

Because -v=3 -vmodule=mysourcefile=6 increases the verbosity threshold in mysourcefile.go to 6 while it is 3 elsewhere.

mysourcefile.go might contains this:

   if loggerV := logger.V(6); loggerV.Enabled(); { loggerV.Info("Log message with details", .... }

Here Enabled must do correct stack unwinding to find mysourcefile.go as caller.

@thockin
Copy link
Contributor

thockin commented Sep 2, 2023

Ahh, I see. It's not that Error() or Info() find the wrong caller, but some (out of tree) implementations of Enabled() need to know the caller.

We can either inline the impl of Enabled() into Info/Error or we can force them all thru a common helper and document why.

Or we can just document it, but that seems unfortunate

@pohly
Copy link
Contributor Author

pohly commented Sep 2, 2023

We have to make it consistent, otherwise LogSink.Enabled won't know how much it should skip. I prefer to not break usage of the more common logger.Info and logger.Error: #216

Let's not decide yet, I first want to test this with klog.

@thockin
Copy link
Contributor

thockin commented Sep 2, 2023

We should not need to break anything - we might need to change the depth that is passed to init

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