Files
yml2213 88d74aca7d 重构日志与可观测性体系
新增单行文本编码器与结构化 GORM 日志,统一错误记录与请求日志策略,收紧日志文件权限并修复按天切分与压缩,支付回调参数脱敏,生产强制阿里云短信,RequestID 校验防注入,日志文案中文化。
2026-07-29 16:19:35 +08:00

37 lines
960 B
Go

package logging
import (
"bytes"
"strings"
"testing"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestTextEncoderProducesReadableSingleLine(t *testing.T) {
var output bytes.Buffer
core := zapcore.NewCore(newTextEncoder(time.UTC), zapcore.AddSync(&output), zapcore.DebugLevel)
logger := zap.New(core)
logger.Info("服务启动",
zap.String("request_id", "req-1"),
zap.String("empty", ""),
zap.String("detail", "line1\nline2"),
)
got := output.String()
if strings.ContainsAny(got, "{}") {
t.Fatalf("output should not use JSON object syntax: %s", got)
}
if !strings.Contains(got, "INFO | 服务启动 | request_id=req-1") {
t.Fatalf("unexpected text output: %s", got)
}
if strings.Contains(got, "empty=") {
t.Fatalf("empty fields should be omitted: %s", got)
}
if strings.Count(got, "\n") != 1 || !strings.Contains(got, `detail="line1\nline2"`) {
t.Fatalf("output should remain one physical line: %q", got)
}
}