Fix log ui accidentally depending on console log level (#13669)
This commit is contained in:
parent
4b6ff7e724
commit
942a4391c5
3 changed files with 41 additions and 14 deletions
15
util/log.go
15
util/log.go
|
|
@ -3,6 +3,7 @@ package util
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -19,10 +20,7 @@ var (
|
||||||
loggersMux sync.Mutex
|
loggersMux sync.Mutex
|
||||||
|
|
||||||
// OutThreshold is the default console log level
|
// OutThreshold is the default console log level
|
||||||
OutThreshold = jww.LevelWarn
|
OutThreshold = jww.LevelInfo
|
||||||
|
|
||||||
// LogThreshold is the default log file level
|
|
||||||
LogThreshold = jww.LevelTrace
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogAreaPadding of log areas
|
// LogAreaPadding of log areas
|
||||||
|
|
@ -60,7 +58,10 @@ func newLogger(area string, lp int) *Logger {
|
||||||
|
|
||||||
level := logLevelForArea(area)
|
level := logLevelForArea(area)
|
||||||
redactor := new(Redactor)
|
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{
|
logger := &Logger{
|
||||||
Notepad: notepad,
|
Notepad: notepad,
|
||||||
|
|
@ -95,7 +96,7 @@ func Loggers(cb func(string, *Logger)) {
|
||||||
func logLevelForArea(area string) jww.Threshold {
|
func logLevelForArea(area string) jww.Threshold {
|
||||||
level, ok := levels[strings.ToLower(area)]
|
level, ok := levels[strings.ToLower(area)]
|
||||||
if !ok {
|
if !ok {
|
||||||
level = LogThreshold
|
level = OutThreshold
|
||||||
}
|
}
|
||||||
return level
|
return level
|
||||||
}
|
}
|
||||||
|
|
@ -103,7 +104,7 @@ func logLevelForArea(area string) jww.Threshold {
|
||||||
// LogLevel sets log level for all loggers
|
// LogLevel sets log level for all loggers
|
||||||
func LogLevel(defaultLevel string, areaLevels map[string]string) {
|
func LogLevel(defaultLevel string, areaLevels map[string]string) {
|
||||||
// default level
|
// default level
|
||||||
LogThreshold = logstash.LogLevelToThreshold(defaultLevel)
|
OutThreshold = logstash.LogLevelToThreshold(defaultLevel)
|
||||||
|
|
||||||
// area levels
|
// area levels
|
||||||
for area, level := range areaLevels {
|
for area, level := range areaLevels {
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -16,7 +16,12 @@ var (
|
||||||
RedactHook = RedactDefaultHook
|
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 {
|
type Redactor struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
redact []string
|
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()
|
l.mu.Lock()
|
||||||
for _, s := range l.redact {
|
for _, s := range l.redact {
|
||||||
p = bytes.ReplaceAll(p, []byte(s), []byte(RedactReplacement))
|
p = bytes.ReplaceAll(p, []byte(s), []byte(RedactReplacement))
|
||||||
}
|
}
|
||||||
l.mu.Unlock()
|
l.mu.Unlock()
|
||||||
return os.Stdout.Write(p)
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedactDefaultHook expands a redaction item to include URL encoding
|
// redactWriter implements a redacting io.Writer
|
||||||
func RedactDefaultHook(s string) []string {
|
type redactWriter struct {
|
||||||
return []string{s, url.QueryEscape(s)}
|
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
16
util/log_test.go
Normal 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)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue