From a8b49568e85770d7e348c17c0681eb4e7413766e Mon Sep 17 00:00:00 2001 From: jmxnzo Date: Mon, 9 Dec 2024 17:25:29 +0100 Subject: [PATCH] cli/genpolicy: continue logging on the latest log level + detect error in stderr (when missing log prefix) --- cli/genpolicy/logtranslator.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cli/genpolicy/logtranslator.go b/cli/genpolicy/logtranslator.go index 8f7d58534..fa343da3c 100644 --- a/cli/genpolicy/logtranslator.go +++ b/cli/genpolicy/logtranslator.go @@ -34,17 +34,37 @@ func (l logTranslator) Write(p []byte) (n int, err error) { } var genpolicyLogPrefixReg = regexp.MustCompile(`^\[[^\]\s]+\s+(\w+)\s+([^\]\s]+)\] (.*)`) +var 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": @@ -56,7 +76,10 @@ func (l logTranslator) startTranslate() { case "DEBUG": l.logger.Debug(match[3], "position", match[2]) } + // Update the latest log level + logLevel = match[1] } + } }() }