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