Skip to content

Commit

Permalink
cli/genpolicy: continue logging on the latest log level + detect erro…
Browse files Browse the repository at this point in the history
…r in stderr (when missing log prefix)
  • Loading branch information
jmxnzo committed Dec 9, 2024
1 parent 637bf99 commit e62f731
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cli/genpolicy/logtranslator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,40 @@ func (l logTranslator) Write(p []byte) (n int, err error) {
return l.w.Write(p)
}

var genpolicyLogPrefixReg = regexp.MustCompile(`^\[[^\]\s]+\s+(\w+)\s+([^\]\s]+)\] (.*)`)
var (
genpolicyLogPrefixReg = regexp.MustCompile(`^\[[^\]\s]+\s+(\w+)\s+([^\]\s]+)\] (.*)`)
errorMessage = regexp.MustCompile(`^thread\s+'main'\s+panicked\s+at`)
)

func (l logTranslator) startTranslate() {
go func() {
defer close(l.stopDoneC)
scanner := bufio.NewScanner(l.r)
// Default log level is initially set to 'WARN', only fallback if first line does not match the logging pattern
logLevel := "WARN"

for scanner.Scan() {
line := scanner.Text()
match := genpolicyLogPrefixReg.FindStringSubmatch(line)
if len(match) != 4 {
// genpolicy prints some warnings without the logger
l.logger.Warn(line)
// we continue logging on the latest used log-level

// Error is written to stderr by genpolicy without using the logger,
// simple regex to detect the error message on stderr
if errorMessage.MatchString(line) {
logLevel = "ERROR"
}
switch logLevel {
case "ERROR":
l.logger.Error(line)
case "WARN":
l.logger.Warn(line)
case "INFO":
fallthrough
case "DEBUG":
l.logger.Debug(line)
}
} else {
switch match[1] {
case "ERROR":
Expand All @@ -56,7 +78,10 @@ func (l logTranslator) startTranslate() {
case "DEBUG":
l.logger.Debug(match[3], "position", match[2])
}
// Update the latest log level
logLevel = match[1]
}

}
}()
}
Expand Down

0 comments on commit e62f731

Please sign in to comment.