-
Notifications
You must be signed in to change notification settings - Fork 18
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
Idea: 'Helper' method for Caller #16
Comments
Do you think it would be possible to implement this idea without changing the implementations of |
What I had in mind would change |
OK. What if instead of changing |
I have an idea, but I'm not yet sure it's feasible. @bboreham @ChrisHines — could you provide a small program that demonstrates the problem cases you've got in mind? |
One real case is where we want to send the same log message to tracing and to a go-kit logger; we have a lot of Similar, but where we do manage to adjust Trivial self-contained program:
|
@bboreham Thank you! |
@bboreham Do I understand correctly that you don't want the current output of
and instead want
? |
Exactly. |
@bboreham since func utility(logger log.Logger, reason string) {
logger.Log("msg", "important "+reason, "caller", log.Caller(4))
} |
How would I know |
You would need to somehow pass this around you utilities, I guess. |
I don't think that is realistic in a large program made up of many packages. |
I'm sharing this experiment rather prematurely in order to solicit feedback. Here's the gist. And here's the gist: The basic idea is to embed more "smarts" into the logged value. The log.Valuer is defined as a method on a Context struct, which holds an optional call frame representing the point in the stack where SetHelper was called. The value, which is lazy-evaluated as in @bboreham's example, consults that state in order to determine which frame to return as the caller. There are a couple of outstanding issues. At the moment, you have to call SetHelper on the Context struct, which basically doesn't solve the problem: you should be able to flag Helper on a log.Logger directly. I think this is solvable, by making the Context a log.Logger itself, and providing a I'll keep poking at this, but if anyone is inspired to take the ball and run with it, please do. Or, if anyone sees a problem I haven't noticed, feedback appreciated. |
Currently, it is necessary to figure out how each log method will be called in order to specify a count of stack frames to
Caller
.log/value.go
Lines 82 to 84 in 5739c26
This makes functions that wrap or forward to
go-kit/log
hard to use.Suggestion: have a Helper method like in
testing
, which tellsCaller
to skip that function from the count.This would be relatively expensive – a stack walk, lock and map lookup each time the helper is entered, and more map lookups when actually logging – but much more pleasant to use.
The text was updated successfully, but these errors were encountered: