fix(logger): Handle trace level for standard log (#15815)

This commit is contained in:
Sven Rebhan 2024-09-03 18:33:51 +02:00 committed by GitHub
parent 44aa976fad
commit 2e00753df7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 22 deletions

View File

@ -2,11 +2,8 @@ package logger
import (
"bytes"
"regexp"
)
var prefixRegex = regexp.MustCompile("^[DIWE]!")
type stdlogRedirector struct {
log logger
}
@ -14,26 +11,21 @@ type stdlogRedirector struct {
func (s *stdlogRedirector) Write(b []byte) (n int, err error) {
msg := bytes.Trim(b, " \t\r\n")
// Extract the log-level indicator; use info by default
loc := prefixRegex.FindIndex(b)
level := 'I'
if loc != nil {
level = rune(b[loc[0]])
msg = bytes.Trim(msg[loc[1]:], " \t\r\n")
}
// Log with the given level
switch level {
case 'T':
s.log.Trace(string(msg))
case 'D':
s.log.Debug(string(msg))
case 'I':
// Check a potential log-level indicator and log with the given level or
// use info by default
switch {
case bytes.HasPrefix(msg, []byte("E! ")):
s.log.Error(string(msg[3:]))
case bytes.HasPrefix(msg, []byte("W! ")):
s.log.Warn(string(msg[3:]))
case bytes.HasPrefix(msg, []byte("I! ")):
s.log.Info(string(msg[3:]))
case bytes.HasPrefix(msg, []byte("D! ")):
s.log.Debug(string(msg[3:]))
case bytes.HasPrefix(msg, []byte("T! ")):
s.log.Trace(string(msg[3:]))
default:
s.log.Info(string(msg))
case 'W':
s.log.Warn(string(msg))
case 'E':
s.log.Error(string(msg))
}
return len(b), nil