feat(parser/log): log string line by line
All checks were successful
submodules sync / sync (push) Successful in 48s
build / build (push) Successful in 3m0s
build / trigger-build-image (push) Successful in 14s

This commit is contained in:
张泊明518370910136 2025-11-27 22:47:52 -08:00
parent e800bc5c2a
commit 91a452e23d
GPG Key ID: D47306D7062CDA9D

View File

@ -5,23 +5,37 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log/slog" "log/slog"
"strings"
"github.com/joint-online-judge/JOJ3/internal/stage" "github.com/joint-online-judge/JOJ3/internal/stage"
) )
func (*Log) parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { func (*Log) parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
content := executorResult.Files[conf.Filename] content, ok := executorResult.Files[conf.Filename]
var data map[string]any if !ok {
slog.Error("file not found for log parser", "filename", conf.Filename)
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf("log parser: file %s not found", conf.Filename),
}
}
contentBytes := []byte(content) contentBytes := []byte(content)
if json.Valid(contentBytes) { var data map[string]any
err := json.Unmarshal(contentBytes, &data) if err := json.Unmarshal(contentBytes, &data); err != nil {
if err != nil { // Not a valid json or failed to unmarshal, log as raw string line by line.
slog.Error(conf.Msg, "error", err) for line := range strings.SplitSeq(content, "\n") {
return stage.ParserResult{ if strings.TrimSpace(line) != "" {
Score: 0, slog.Default().Log(
Comment: fmt.Sprintf("Failed to parse content: %s", err), context.Background(),
slog.Level(conf.Level),
conf.Msg,
"line",
line,
)
} }
} }
} else {
// Valid json, log as key-value pairs.
args := make([]any, 0, len(data)*2) args := make([]any, 0, len(data)*2)
for key, value := range data { for key, value := range data {
args = append(args, key, value) args = append(args, key, value)
@ -32,14 +46,6 @@ func (*Log) parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserRe
conf.Msg, conf.Msg,
args..., args...,
) )
} else {
slog.Default().Log(
context.Background(),
slog.Level(conf.Level),
conf.Msg,
"content",
content,
)
} }
return stage.ParserResult{ return stage.ParserResult{
Score: 0, Score: 0,