log: split out stackEnabled field, to not conflate features

This commit is contained in:
Martin Holst Swende 2023-09-07 11:33:33 +02:00
parent 81f5cbd333
commit d9e335b2be
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
4 changed files with 35 additions and 9 deletions

View file

@ -33,8 +33,16 @@ var locationTrims = []string{
// format output.
func PrintOrigins(print bool) {
locationEnabled.Store(print)
if print {
stackEnabled.Store(true)
}
}
// storeStackRecords is an atomic flag controlling whether the log handler needs
// to store the callsite stack. This is needed in case any handler wants to
// print locations (locationEnabled), use vmodule, or print full stacks (BacktraceAt).
var stackEnabled atomic.Bool
// locationEnabled is an atomic flag controlling whether the terminal formatter
// should append the log locations too when printing entries.
var locationEnabled atomic.Bool

View file

@ -141,7 +141,7 @@ func (h *GlogHandler) Vmodule(ruleset string) error {
h.override.Store(len(filter) != 0)
// Enable location storage (globally)
if len(h.patterns) > 0 {
locationEnabled.Store(true)
stackEnabled.Store(true)
}
return nil
}
@ -176,7 +176,7 @@ func (h *GlogHandler) BacktraceAt(location string) error {
h.location = location
h.backtrace.Store(len(location) > 0)
// Enable location storage (globally)
locationEnabled.Store(true)
stackEnabled.Store(true)
return nil
}

View file

@ -189,7 +189,7 @@ func (l *logger) write(msg string, lvl Lvl, ctx []interface{}, skip int) {
Ctx: ctxKey,
},
}
if locationEnabled.Load() {
if stackEnabled.Load() {
record.Call = stack.Caller(skip)
}
l.h.Log(record)

View file

@ -28,7 +28,7 @@ func (n notimeHandler) Log(r *Record) error {
// TestLoggingWithTrace checks that if BackTraceAt is set, then the
// gloghandler is capable of spitting out a stacktrace
func TestLoggingWithTrace(t *testing.T) {
defer locationEnabled.Store(locationEnabled.Load())
defer stackEnabled.Store(stackEnabled.Load())
out := new(bytes.Buffer)
logger := New()
{
@ -41,10 +41,28 @@ func TestLoggingWithTrace(t *testing.T) {
}
logger.Trace("a message", "foo", "bar") // Will be bumped to INFO
have := out.String()
wantPrefix := `INFO [01-01|01:00:00.000|log/logger_test.go:59] a message
goroutine`
if len(have) < len(wantPrefix) || strings.HasPrefix(have, wantPrefix) {
t.Errorf("\nhave: '%v'\nwant: '%v'\n", have, wantPrefix)
wantPrefix := "INFO [01-01|01:00:00.000] a message\n\ngoroutine"
if len(have) < len(wantPrefix) || !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(notimeHandler{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()
want := "TRACE[01-01|01:00:00.000] a message foo=bar\n"
if have != want {
t.Errorf("\nhave: %q\nwant: %q\n", have, want)
}
}