This commit is contained in:
yun
2026-09-14 00:14:30 +08:00
parent 316c6420a7
commit 7e1b474823
20 changed files with 1885 additions and 166 deletions
+29 -2
View File
@@ -3,6 +3,7 @@ package loggerx_test
import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"os"
@@ -145,8 +146,9 @@ func TestCompressedArchiveIsReadable(t *testing.T) {
// 压缩归档 + 当前未压缩文件一起统计,必须一条不漏
content := readAllGz(t, dir) + readAllLogs(t, dir)
if !strings.Contains(content, "[info]{") {
t.Errorf("内容不像日志: %.120q", content)
// 落到磁盘的每条记录都应是合法 JSON 行(便于采集端解析)
if !jsonLinesValid(t, content) {
t.Errorf("归档内容不是合法 JSON 行: %.160q", content)
}
missing := 0
for i := 0; i < n; i++ {
@@ -274,6 +276,31 @@ func TestSizeSplitOnRestart(t *testing.T) {
}
}
// jsonLinesValid 校验每行都是合法 JSON(空行忽略)
func jsonLinesValid(t *testing.T, content string) bool {
t.Helper()
lines := strings.Split(strings.TrimSpace(content), "\n")
if len(lines) == 0 {
return false
}
for i, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
var v map[string]any
if err := json.Unmarshal([]byte(line), &v); err != nil {
t.Logf("第 %d 行不是合法 JSON: %v\n%s", i+1, err, line)
return false
}
if v["level"] == nil {
t.Logf("第 %d 行缺少 level 字段: %s", i+1, line)
return false
}
}
return true
}
// readAllGz 解开目录下所有 .gz 归档
func readAllGz(t *testing.T, dir string) string {
t.Helper()