|
| 1 | +package oopszap |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/samber/oops" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.uber.org/zap" |
| 12 | + "go.uber.org/zap/zapcore" |
| 13 | +) |
| 14 | + |
| 15 | +type jsonLogEntryError struct { |
| 16 | + Error string `json:"error"` |
| 17 | + Time string `json:"time"` |
| 18 | + Domain string `json:"domain"` |
| 19 | + Trace string `json:"trace"` |
| 20 | + Context map[string]any `json:"context"` |
| 21 | +} |
| 22 | + |
| 23 | +type jsonLogEntry struct { |
| 24 | + Level string `json:"level"` |
| 25 | + Stacktrace string `json:"stacktrace"` |
| 26 | + Message string `json:"msg"` |
| 27 | + Error jsonLogEntryError `json:"error"` |
| 28 | +} |
| 29 | + |
| 30 | +func TestZapFormatter(t *testing.T) { |
| 31 | + is := assert.New(t) |
| 32 | + |
| 33 | + // Setup Zap logger buffer |
| 34 | + buffer := &bytes.Buffer{} |
| 35 | + encoderConfig := zap.NewProductionEncoderConfig() |
| 36 | + encoderConfig.TimeKey = "" // disable timestamp for easier testing or keep it? zerolog test kept it but cleared it. |
| 37 | + // We want to test the payload structure. |
| 38 | + |
| 39 | + core := zapcore.NewCore( |
| 40 | + zapcore.NewJSONEncoder(encoderConfig), |
| 41 | + zapcore.AddSync(buffer), |
| 42 | + zap.ErrorLevel, |
| 43 | + ) |
| 44 | + logger := zap.New(core) |
| 45 | + |
| 46 | + err := oops. |
| 47 | + In("test"). |
| 48 | + With("driver", "postgresql"). |
| 49 | + Errorf("could not fetch user") |
| 50 | + |
| 51 | + logger.Error("something went wrong", |
| 52 | + zap.Object("error", OopsMarshalFunc(err)), |
| 53 | + zap.String("stacktrace", OopsStackMarshaller(err)), |
| 54 | + ) |
| 55 | + |
| 56 | + var loggedError jsonLogEntry |
| 57 | + decErr := json.Unmarshal(buffer.Bytes(), &loggedError) |
| 58 | + require.NoError(t, decErr) |
| 59 | + |
| 60 | + // Assertions |
| 61 | + is.Contains(loggedError.Stacktrace, "Oops: could not fetch user\n --- at ") |
| 62 | + is.NotEmpty(loggedError.Error.Time) |
| 63 | + is.NotEmpty(loggedError.Error.Trace) |
| 64 | + |
| 65 | + // Clear dynamic fields for equality check |
| 66 | + loggedError.Stacktrace = "" |
| 67 | + loggedError.Error.Time = "" |
| 68 | + loggedError.Error.Trace = "" |
| 69 | + |
| 70 | + expected := jsonLogEntry{ |
| 71 | + Level: "error", |
| 72 | + Message: "something went wrong", |
| 73 | + Error: jsonLogEntryError{ |
| 74 | + Error: "could not fetch user", |
| 75 | + Domain: "test", |
| 76 | + Context: map[string]any{ |
| 77 | + "driver": "postgresql", |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + is.Equal(expected, loggedError) |
| 82 | +} |
| 83 | + |
| 84 | +func BenchmarkZapFormatter(b *testing.B) { |
| 85 | + err := oops. |
| 86 | + In("test"). |
| 87 | + With("driver", "postgresql"). |
| 88 | + Errorf("could not fetch user") |
| 89 | + |
| 90 | + // Discard output |
| 91 | + core := zapcore.NewCore( |
| 92 | + zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), |
| 93 | + zapcore.AddSync(bytes.NewBuffer(nil)), |
| 94 | + zap.ErrorLevel, |
| 95 | + ) |
| 96 | + logger := zap.New(core) |
| 97 | + |
| 98 | + b.ResetTimer() |
| 99 | + b.ReportAllocs() |
| 100 | + |
| 101 | + for i := 0; i < b.N; i++ { |
| 102 | + logger.Error("something went wrong", |
| 103 | + zap.Object("error", OopsMarshalFunc(err)), |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
0 commit comments