|
3 | 3 | package zap_test |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "errors" |
6 | 7 | "testing" |
7 | 8 |
|
8 | 9 | "github.com/hellofresh/goengine" |
9 | | - |
10 | 10 | zapExtension "github.com/hellofresh/goengine/extension/zap" |
| 11 | + "github.com/stretchr/testify/assert" |
11 | 12 | "go.uber.org/zap" |
| 13 | + "go.uber.org/zap/zapcore" |
| 14 | + "go.uber.org/zap/zaptest/observer" |
12 | 15 | ) |
13 | 16 |
|
| 17 | +func TestWrap_LogEntry(t *testing.T) { |
| 18 | + zapCore, logObserver := observer.New(zapcore.DebugLevel) |
| 19 | + logger := zapExtension.Wrap(zap.New(zapCore)) |
| 20 | + |
| 21 | + testCases := []struct { |
| 22 | + msg string |
| 23 | + fields func(goengine.LoggerEntry) |
| 24 | + |
| 25 | + expectedMsg string |
| 26 | + expectedContext map[string]interface{} |
| 27 | + }{ |
| 28 | + { |
| 29 | + "test nil fields", |
| 30 | + nil, |
| 31 | + "test nil fields", |
| 32 | + map[string]interface{}{}, |
| 33 | + }, |
| 34 | + { |
| 35 | + "test with fields", |
| 36 | + func(e goengine.LoggerEntry) { |
| 37 | + e.String("test", "a value") |
| 38 | + e.Int("normal_int", 99) |
| 39 | + e.Int64("int_64", 2) |
| 40 | + e.Error(errors.New("some error")) |
| 41 | + e.Any("obj", struct { |
| 42 | + test string |
| 43 | + }{test: "test property"}) |
| 44 | + }, |
| 45 | + "test with fields", |
| 46 | + map[string]interface{}{ |
| 47 | + "test": "a value", |
| 48 | + "normal_int": int64(99), |
| 49 | + "int_64": int64(2), |
| 50 | + "error": "some error", |
| 51 | + "obj": struct { |
| 52 | + test string |
| 53 | + }{test: "test property"}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, testCase := range testCases { |
| 59 | + t.Run(testCase.msg, func(t *testing.T) { |
| 60 | + logger.Error(testCase.msg, testCase.fields) |
| 61 | + logger.Warn(testCase.msg, testCase.fields) |
| 62 | + logger.Info(testCase.msg, testCase.fields) |
| 63 | + logger.Debug(testCase.msg, testCase.fields) |
| 64 | + |
| 65 | + logs := logObserver.TakeAll() |
| 66 | + |
| 67 | + if !assert.Len(t, logs, 4) { |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + levelOrder := []zapcore.Level{ |
| 72 | + zapcore.ErrorLevel, |
| 73 | + zapcore.WarnLevel, |
| 74 | + zapcore.InfoLevel, |
| 75 | + zapcore.DebugLevel, |
| 76 | + } |
| 77 | + |
| 78 | + for i, level := range levelOrder { |
| 79 | + assert.Equal(t, level, logs[i].Level) |
| 80 | + assert.Equal(t, testCase.expectedMsg, logs[i].Message) |
| 81 | + assert.Equal(t, testCase.expectedContext, logs[i].ContextMap()) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + t.Run("Do not log disabled levels", func(t *testing.T) { |
| 87 | + zapCore, logObserver := observer.New(zapcore.InfoLevel) |
| 88 | + logger := zapExtension.Wrap(zap.New(zapCore)) |
| 89 | + |
| 90 | + logger.Debug("should not be logged", func(e goengine.LoggerEntry) { |
| 91 | + t.Error("fields should not be called") |
| 92 | + }) |
| 93 | + |
| 94 | + assert.Equal(t, 0, logObserver.Len()) |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func TestWrapper_WithFields(t *testing.T) { |
| 99 | + zapCore, logObserver := observer.New(zapcore.DebugLevel) |
| 100 | + logger := zapExtension.Wrap(zap.New(zapCore)) |
| 101 | + |
| 102 | + t.Run("With fields", func(t *testing.T) { |
| 103 | + loggerWithFields := logger.WithFields(func(e goengine.LoggerEntry) { |
| 104 | + e.String("with field", "check") |
| 105 | + e.String("val", "default") |
| 106 | + }) |
| 107 | + |
| 108 | + loggerWithFields.Debug("test with nil", nil) |
| 109 | + loggerWithFields.Debug("test with override", func(e goengine.LoggerEntry) { |
| 110 | + e.String("val", "override") |
| 111 | + }) |
| 112 | + |
| 113 | + logs := logObserver.TakeAll() |
| 114 | + if assert.Len(t, logs, 2) { |
| 115 | + assert.Equal(t, "test with nil", logs[0].Message) |
| 116 | + assert.Equal(t, map[string]interface{}{ |
| 117 | + "with field": "check", |
| 118 | + "val": "default", |
| 119 | + }, logs[0].ContextMap()) |
| 120 | + |
| 121 | + assert.Equal(t, "test with override", logs[1].Message) |
| 122 | + assert.Equal(t, map[string]interface{}{ |
| 123 | + "with field": "check", |
| 124 | + "val": "override", |
| 125 | + }, logs[1].ContextMap()) |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + t.Run("With fields nil", func(t *testing.T) { |
| 130 | + loggerWithFields := logger.WithFields(nil) |
| 131 | + assert.Equal(t, logger, loggerWithFields) |
| 132 | + }) |
| 133 | +} |
| 134 | + |
14 | 135 | func BenchmarkStandardLoggerEntry(b *testing.B) { |
15 | 136 | b.ReportAllocs() |
16 | 137 |
|
|
0 commit comments