Fix log ui accidentally depending on console log level (#13669)

This commit is contained in:
andig 2024-05-01 13:15:50 +02:00 • committed by GitHub
parent 4b6ff7e724
commit 942a4391c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 14 deletions

View file

@ -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 {

View file

@ -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))
}

16
util/log_test.go Normal file
View file

@ -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)
}