From 942a4391c584e360ece29bd891fb07bad004269a Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 1 May 2024 13:15:50 +0200 Subject: [PATCH] Fix log ui accidentally depending on console log level (#13669) --- util/log.go | 15 ++++++++------- util/{redactor.go => log_redactor.go} | 24 +++++++++++++++++------- util/log_test.go | 16 ++++++++++++++++ 3 files changed, 41 insertions(+), 14 deletions(-) rename util/{redactor.go => log_redactor.go} (74%) create mode 100644 util/log_test.go diff --git a/util/log.go b/util/log.go index c70297a2d..f9458f446 100644 --- a/util/log.go +++ b/util/log.go @@ -3,6 +3,7 @@ package util import ( "io" "log" + "os" "regexp" "strconv" "strings" @@ -19,10 +20,7 @@ var ( loggersMux sync.Mutex // OutThreshold is the default console log level - OutThreshold = jww.LevelWarn - - // LogThreshold is the default log file level - LogThreshold = jww.LevelTrace + OutThreshold = jww.LevelInfo ) // LogAreaPadding of log areas @@ -60,7 +58,10 @@ func newLogger(area string, lp int) *Logger { level := logLevelForArea(area) redactor := new(Redactor) - notepad := jww.NewNotepad(level, level, redactor, logstash.DefaultHandler, padded, log.Ldate|log.Ltime) + notepad := jww.NewNotepad( + level, jww.LevelTrace, + &redactWriter{os.Stdout, redactor}, &redactWriter{logstash.DefaultHandler, redactor}, + padded, log.Ldate|log.Ltime) logger := &Logger{ Notepad: notepad, @@ -95,7 +96,7 @@ func Loggers(cb func(string, *Logger)) { func logLevelForArea(area string) jww.Threshold { level, ok := levels[strings.ToLower(area)] if !ok { - level = LogThreshold + level = OutThreshold } return level } @@ -103,7 +104,7 @@ func logLevelForArea(area string) jww.Threshold { // LogLevel sets log level for all loggers func LogLevel(defaultLevel string, areaLevels map[string]string) { // default level - LogThreshold = logstash.LogLevelToThreshold(defaultLevel) + OutThreshold = logstash.LogLevelToThreshold(defaultLevel) // area levels for area, level := range areaLevels { diff --git a/util/redactor.go b/util/log_redactor.go similarity index 74% rename from util/redactor.go rename to util/log_redactor.go index 13cb49c43..3b27df44b 100644 --- a/util/redactor.go +++ b/util/log_redactor.go @@ -2,8 +2,8 @@ package util import ( "bytes" + "io" "net/url" - "os" "sync" ) @@ -16,7 +16,12 @@ var ( RedactHook = RedactDefaultHook ) -// Redactor implements a redacting io.Writer +// RedactDefaultHook expands a redaction item to include URL encoding +func RedactDefaultHook(s string) []string { + return []string{s, url.QueryEscape(s)} +} + +// Redactor implements log redaction type Redactor struct { mu sync.Mutex redact []string @@ -34,16 +39,21 @@ func (l *Redactor) Redact(redact ...string) { } } -func (l *Redactor) Write(p []byte) (n int, err error) { +func (l *Redactor) redacted(p []byte) []byte { l.mu.Lock() for _, s := range l.redact { p = bytes.ReplaceAll(p, []byte(s), []byte(RedactReplacement)) } l.mu.Unlock() - return os.Stdout.Write(p) + return p } -// RedactDefaultHook expands a redaction item to include URL encoding -func RedactDefaultHook(s string) []string { - return []string{s, url.QueryEscape(s)} +// redactWriter implements a redacting io.Writer +type redactWriter struct { + w io.Writer + r *Redactor +} + +func (w *redactWriter) Write(p []byte) (n int, err error) { + return w.w.Write(w.r.redacted(p)) } diff --git a/util/log_test.go b/util/log_test.go new file mode 100644 index 000000000..f458ae960 --- /dev/null +++ b/util/log_test.go @@ -0,0 +1,16 @@ +package util + +import ( + "testing" + + "github.com/evcc-io/evcc/util/logstash" + jww "github.com/spf13/jwalterweatherman" + "github.com/stretchr/testify/require" +) + +func TestLogger(t *testing.T) { + log := NewLogger("test") + log.TRACE.Print("foo") + + require.Len(t, logstash.All(nil, jww.LevelTrace, 0), 1) +}