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) } }