优化结构

This commit is contained in:
Yvan
2026-08-21 16:52:37 +08:00
parent e22301f8f6
commit ba1c73fa66
5 changed files with 33 additions and 23 deletions
+16
View File
@@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@@ -61,6 +62,21 @@ func TestZapHandlerRoutesHTTPAndErrorLogs(t *testing.T) {
}
}
func TestConsoleEncoderDoesNotChangeJSONFileEncoder(t *testing.T) {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.RFC3339NanoTimeEncoder
fileEncoder := zapcore.NewJSONEncoder(config)
entry := zapcore.Entry{Level: zapcore.InfoLevel, Time: time.Date(2026, 8, 21, 16, 30, 0, 0, time.Local), Message: "started"}
buffer, err := fileEncoder.EncodeEntry(entry, nil)
if err != nil {
t.Fatal(err)
}
defer buffer.Free()
if !strings.HasPrefix(buffer.String(), "{") || !strings.Contains(buffer.String(), `"msg":"started"`) {
t.Fatalf("file encoder is no longer JSON: %s", buffer.String())
}
}
func TestZapHandlerRecordsEveryErrorThroughSink(t *testing.T) {
root := t.TempDir()
var entries []ErrorEntry