-
Notifications
You must be signed in to change notification settings - Fork 273
Add ExtraCalldepth to Logger #42
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
Conversation
ExtraCalldepth is added to the default calldepth of 2 to allow the correct function name to be shown when exposing logger methods at package level or wrapping them in utility functions Add an example of how this works to examples/example.go Update examples/example.go to include a new screenshot Update README.md to include the new example.go
Hi @op I've added documentation per your request; I made an attempt at adding a test, but since the memory backend seems to ignore the calldepth, my approach didn't seem to achieve much, as Formatted(calldepth) is called in the tests. This attempt, for example, doesn't achieve much as it passes for any value of ExtraCalldepth: func TestExtraCalldepth(t *testing.T) {
backend := InitForTesting(DEBUG)
// Just output the shortfunc; that's all we care about in this test
backendFormatter := NewBackendFormatter(
backend,
MustStringFormatter("%{shortfunc}"),
)
SetBackend(backendFormatter)
log := MustGetLogger("test")
wrappedError := log.Error
log.ExtraCalldepth = 1
wrappedError("")
if "TestExtraCalldepth" != MemoryRecordN(backend, 0).Formatted(0) {
t.Error("Unexpected shortfunc name")
}
} If you have any suggestions as to a better test strategy, let me know and I'll give it another whack; I'm inexperienced with testing in golang, I'm afraid. |
// Example format string. Everything except the message has a custom color | ||
// which is dependent on the log level. Many fields have a custom output | ||
// formatting too, eg. the time returns the hour down to the milli second. | ||
var format = logging.MustStringFormatter( | ||
"%{color}%{time:15:04:05.000000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}", | ||
"%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I'd note that this change was caused by me copying the updated contents of example.go directly into the code block; it looks like it was updated in an earlier commit but not amended in the code block in README.md
Looks like the example doesn't work in go v1... since this isn't a compulsory feature, though, I'd suggest support for >= 1.1 is better than nothing. The v1.1-only part could be split into a secondary example and excluded from the 1.0 build maybe? |
+1 to ExtraCalldepth |
All code looks good! I will merge this, but will keep the ExtraCallDepth away from the normal example. This seems like an advanced feature that will confuse first time users. Thanks! |
ExtraCalldepth is added to the default calldepth of 2 to allow the
correct function name to be shown when exposing logger methods at
package level or wrapping them in utility functions
Add an example of how this works to examples/example.go
Update examples/example.go to include a new screenshot
Update README.md to include the new example.go