-
Notifications
You must be signed in to change notification settings - Fork 20.1k
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
log: avoid stack lookups when not needed/used #28069
Changes from all commits
06d91bd
99c123c
8d96927
81f5cbd
d9e335b
00f8abc
8e17782
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,19 +177,22 @@ type logger struct { | |
} | ||
|
||
func (l *logger) write(msg string, lvl Lvl, ctx []interface{}, skip int) { | ||
l.h.Log(&Record{ | ||
record := &Record{ | ||
Time: time.Now(), | ||
Lvl: lvl, | ||
Msg: msg, | ||
Ctx: newContext(l.ctx, ctx), | ||
Call: stack.Caller(skip), | ||
KeyNames: RecordKeyNames{ | ||
Time: timeKey, | ||
Msg: msgKey, | ||
Lvl: lvlKey, | ||
Ctx: ctxKey, | ||
}, | ||
}) | ||
} | ||
if stackEnabled.Load() { | ||
record.Call = stack.Caller(skip) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's problematic. e.g. in the But I agree it's a good optimization, but just need more checks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, interesting. I dug into this, it turns out that // String implements fmt.Stinger. It is equivalent to fmt.Sprintf("%v", c).
func (c Call) String() string {
return fmt.Sprint(c)
} So there's no crash, just it won't print the backtrace. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be noted, though: the type Call struct {
frame runtime.Frame
} (no pointers) type Frame struct {
// PC is the program counter for the location in this frame.
// For a frame that calls another frame, this will be the
// program counter of a call instruction. Because of inlining,
// multiple frames may have the same PC value, but different
// symbolic information.
PC uintptr
... This struct too is (nearly) pointer-free. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha I spent two hours yesterday trying to create a test to hit this, but couldn't because no pointer. So it looks good to me |
||
} | ||
l.h.Log(record) | ||
} | ||
|
||
func (l *logger) New(ctx ...interface{}) Logger { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// TestLoggingWithTrace checks that if BackTraceAt is set, then the | ||
// gloghandler is capable of spitting out a stacktrace | ||
func TestLoggingWithTrace(t *testing.T) { | ||
defer stackEnabled.Store(stackEnabled.Load()) | ||
out := new(bytes.Buffer) | ||
logger := New() | ||
{ | ||
glog := NewGlogHandler(StreamHandler(out, TerminalFormat(false))) | ||
glog.Verbosity(LvlTrace) | ||
if err := glog.BacktraceAt("logger_test.go:24"); err != nil { | ||
t.Fatal(err) | ||
} | ||
logger.SetHandler(glog) | ||
} | ||
logger.Trace("a message", "foo", "bar") // Will be bumped to INFO | ||
have := out.String() | ||
if !strings.HasPrefix(have, "INFO") { | ||
t.Fatalf("backtraceat should bump level to info: %s", have) | ||
} | ||
// The timestamp is locale-dependent, so we want to trim that off | ||
// "INFO [01-01|00:00:00.000] a messag ..." -> "a messag..." | ||
have = strings.Split(have, "]")[1] | ||
wantPrefix := " a message\n\ngoroutine" | ||
if !strings.HasPrefix(have, wantPrefix) { | ||
t.Errorf("\nhave: %q\nwant: %q\n", have, wantPrefix) | ||
} | ||
} | ||
|
||
// TestLoggingWithVmodule checks that vmodule works. | ||
func TestLoggingWithVmodule(t *testing.T) { | ||
defer stackEnabled.Store(stackEnabled.Load()) | ||
out := new(bytes.Buffer) | ||
logger := New() | ||
{ | ||
glog := NewGlogHandler(StreamHandler(out, TerminalFormat(false))) | ||
glog.Verbosity(LvlCrit) | ||
logger.SetHandler(glog) | ||
logger.Warn("This should not be seen", "ignored", "true") | ||
glog.Vmodule("logger_test.go=5") | ||
} | ||
logger.Trace("a message", "foo", "bar") | ||
have := out.String() | ||
// The timestamp is locale-dependent, so we want to trim that off | ||
// "INFO [01-01|00:00:00.000] a messag ..." -> "a messag..." | ||
have = strings.Split(have, "]")[1] | ||
want := " a message foo=bar\n" | ||
if have != want { | ||
t.Errorf("\nhave: %q\nwant: %q\n", have, want) | ||
} | ||
} | ||
|
||
func BenchmarkTraceLogging(b *testing.B) { | ||
Root().SetHandler(LvlFilterHandler(LvlInfo, StreamHandler(os.Stderr, TerminalFormat(true)))) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
Trace("a message", "v", i) | ||
} | ||
} |
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.
linter's gonna lint :P field and comment name don't match