Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions logger/stdlog_redirector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,30 @@ package logger

import (
"bytes"
"regexp"
)

var prefixRegex = regexp.MustCompile("^[DIWE]!")

type stdlogRedirector struct {
log logger
}

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
Expand Down
Loading