log: avoid stack lookups when not needed/used

This commit is contained in:
Martin Holst Swende 2023-09-06 14:48:09 +02:00
parent 25733a4aad
commit 06d91bdd9f
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 20 additions and 3 deletions

View file

@ -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 locationEnabled.Load() {
record.Call = stack.Caller(skip)
}
l.h.Log(record)
}
func (l *logger) New(ctx ...interface{}) Logger {

14
log/logger_test.go Normal file
View file

@ -0,0 +1,14 @@
package log
import (
"os"
"testing"
)
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)
}
}