github.com/yandex/pandora@v0.5.32/lib/zaputil/stack_extract_core_test.go (about) 1 package zaputil 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/pkg/errors" 8 "github.com/stretchr/testify/assert" 9 "go.uber.org/zap" 10 "go.uber.org/zap/zapcore" 11 "go.uber.org/zap/zaptest/observer" 12 ) 13 14 func noStackFields1() []zapcore.Field { 15 return []zapcore.Field{ 16 zap.String("0", "0"), zap.Error(fmt.Errorf("fields 1")), 17 } 18 } 19 20 func noStackFields2() []zapcore.Field { 21 return []zapcore.Field{ 22 zap.String("1", "1"), zap.Error(fmt.Errorf("fields 2")), 23 } 24 } 25 26 func Test_StackExtractCore(t *testing.T) { 27 t.Run("check integration", func(t *testing.T) { 28 nested, logs := observer.New(zap.DebugLevel) 29 log := zap.New(NewStackExtractCore(nested)) 30 31 log.Debug("test", noStackFields1()...) 32 assert.Equal(t, 1, logs.Len()) 33 entry := logs.All()[0] 34 assert.Equal(t, "test", entry.Message) 35 assert.Equal(t, noStackFields1(), entry.Context) 36 }) 37 38 t.Run("no stacks", func(t *testing.T) { 39 nested, logs := observer.New(zap.DebugLevel) 40 testee := NewStackExtractCore(nested) 41 42 testee = testee.With(noStackFields1()) 43 entry := zapcore.Entry{Message: "test"} 44 _ = testee.Write(entry, noStackFields2()) 45 46 assert.Equal(t, 1, logs.Len()) 47 assert.Equal( 48 t, 49 observer.LoggedEntry{Entry: entry, Context: append(noStackFields1(), noStackFields2()...)}, 50 logs.All()[0], 51 ) 52 }) 53 54 t.Run("stack in write", func(t *testing.T) { 55 const sampleErrMsg = "stacked error msg" 56 sampleErr := errors.New(sampleErrMsg) 57 sampleStack := fmt.Sprintf("%+v", sampleErr.(stackedErr).StackTrace()) 58 59 nested, logs := observer.New(zap.DebugLevel) 60 testee := NewStackExtractCore(nested) 61 62 fields := append(noStackFields1(), zap.Error(sampleErr)) 63 fieldsCopy := make([]zapcore.Field, len(fields)) 64 copy(fieldsCopy, fields) 65 entry := zapcore.Entry{Message: "test"} 66 _ = testee.Write(entry, fields) 67 68 expectedEntry := entry 69 expectedEntry.Stack = "error stacktrace:" + sampleStack 70 assert.Equal(t, 1, logs.Len()) 71 assert.Equal( 72 t, 73 observer.LoggedEntry{ 74 Entry: expectedEntry, 75 Context: append(noStackFields1(), zap.String("error", sampleErrMsg)), 76 }, 77 logs.All()[0], 78 ) 79 assert.Equal(t, fieldsCopy, fields) 80 }) 81 82 t.Run("stack in with", func(t *testing.T) { 83 const sampleErrMsg = "stacked error msg" 84 sampleCause := fmt.Errorf(sampleErrMsg) 85 sampleErr := errors.WithStack(sampleCause) 86 sampleStack := fmt.Sprintf("%+v", sampleErr.(stackedErr).StackTrace()) 87 88 nested, logs := observer.New(zap.DebugLevel) 89 testee := NewStackExtractCore(nested) 90 91 fields := append(noStackFields1(), zap.Error(sampleErr)) 92 fieldsCopy := make([]zapcore.Field, len(fields)) 93 copy(fieldsCopy, fields) 94 entry := zapcore.Entry{Message: "test"} 95 testee = testee.With(fields) 96 _ = testee.Write(entry, nil) 97 98 expectedEntry := entry 99 expectedEntry.Stack = "error stacktrace:" + sampleStack 100 assert.Equal(t, 1, logs.Len()) 101 assert.Equal( 102 t, 103 observer.LoggedEntry{ 104 Entry: expectedEntry, 105 Context: append(noStackFields1(), zap.Error(sampleCause)), 106 }, 107 logs.All()[0], 108 ) 109 assert.Equal(t, fieldsCopy, fields) 110 }) 111 112 t.Run("stacks join", func(t *testing.T) { 113 const sampleErrMsg = "stacked error msg" 114 sampleErr := errors.New(sampleErrMsg) 115 sampleStack := fmt.Sprintf("%+v", sampleErr.(stackedErr).StackTrace()) 116 117 nested, logs := observer.New(zap.DebugLevel) 118 testee := NewStackExtractCore(nested) 119 120 const entryStack = "entry stack" 121 entry := zapcore.Entry{Message: "test", Stack: entryStack} 122 const customKey = "custom-key" 123 _ = testee.Write(entry, []zapcore.Field{zap.NamedError(customKey, sampleErr)}) 124 125 expectedEntry := entry 126 expectedEntry.Stack = entryStack + "\n" + customKey + " stacktrace:" + sampleStack 127 assert.Equal(t, 1, logs.Len()) 128 129 assert.Equal( 130 t, 131 observer.LoggedEntry{ 132 Entry: expectedEntry, 133 Context: []zapcore.Field{zap.String(customKey, sampleErrMsg)}, 134 }, 135 logs.All()[0], 136 ) 137 }) 138 }