Skip to content

Commit

Permalink
Fix log ui accidentally depending on console log level (#13669)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored May 1, 2024
1 parent 4b6ff7e commit 942a439
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
15 changes: 8 additions & 7 deletions util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"io"
"log"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -95,15 +96,15 @@ 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
}

// 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 {
Expand Down
24 changes: 17 additions & 7 deletions util/redactor.go → util/log_redactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package util

import (
"bytes"
"io"
"net/url"
"os"
"sync"
)

Expand All @@ -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
Expand All @@ -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 changes: 16 additions & 0 deletions util/log_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 942a439

Please sign in to comment.