github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/zlog/logr_test.go (about) 1 package zlog 2 3 import ( 4 "bufio" 5 "bytes" 6 "fmt" 7 "io" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "go.uber.org/zap" 13 "go.uber.org/zap/zapcore" 14 ) 15 16 func newTestLogrLogger(lv Level, w io.Writer) *zap.Logger { 17 if w == nil { 18 w = io.Discard 19 } 20 cfg := &Config{ 21 Level: lv.String(), 22 Format: "json", 23 DisableTimestamp: true, 24 DisableStacktrace: true, 25 } 26 logger, _, err := NewWithOutput(cfg, zapcore.AddSync(w)) 27 if err != nil { 28 panic(err) 29 } 30 return logger 31 } 32 33 func TestNewLogrLogger(t *testing.T) { 34 r0 := NewLogrLogger() 35 assert.NotNil(t, r0.GetSink().(*logrImpl).opts) 36 assert.NotNil(t, r0.GetSink().(*logrImpl).l) 37 38 r1 := NewLogrLogger(func(opts *LogrOptions) { 39 opts.ErrorKey = "err" 40 }) 41 assert.Equal(t, "err", r1.GetSink().(*logrImpl).opts.ErrorKey) 42 assert.NotNil(t, r1.GetSink().(*logrImpl).l) 43 r11 := NewLogrLogger(func(opts *LogrOptions) { 44 opts.ErrorKey = "err" 45 }) 46 assert.Equal(t, "err", r11.GetSink().(*logrImpl).opts.ErrorKey) 47 assert.NotNil(t, r1.GetSink().(*logrImpl).l) 48 49 l := L().With(zap.String("ns", "default")) 50 r2 := NewLogrLogger(func(opts *LogrOptions) { 51 opts.Logger = l.Logger 52 }) 53 r2.Info("test NewLogrLogger with logger") 54 } 55 56 func TestLogrLoggerInfo(t *testing.T) { 57 var buffer bytes.Buffer 58 writer := bufio.NewWriter(&buffer) 59 zl := newTestLogrLogger(TraceLevel, writer) 60 testLogger := NewLogrLogger(func(opts *LogrOptions) { 61 opts.Logger = zl 62 }) 63 64 testLogger.Info("test info", "ns", "default", "podnum", 2) 65 err := writer.Flush() 66 require.Nil(t, err) 67 68 logStr := buffer.String() 69 assert.Contains(t, logStr, `"level":"info"`) 70 assert.Contains(t, logStr, `"caller":"zlog/logr_test.go:`) 71 assert.Contains(t, logStr, `"msg":"test info"`) 72 assert.Contains(t, logStr, `"ns":"default"`) 73 assert.Contains(t, logStr, `"podnum":2`) 74 75 // test invalid log 76 buffer.Reset() 77 testLogger.Info("invalid", zap.String("ns", "default"), 12345) 78 err = writer.Flush() 79 require.Nil(t, err) 80 81 logStr = buffer.String() 82 fmt.Println(logStr) 83 assert.Contains(t, logStr, `"level":"dpanic"`) 84 assert.Contains(t, logStr, `"caller":"zlog/logr_test.go:`) 85 assert.Contains(t, logStr, `"msg":"invalid"`) 86 assert.Contains(t, logStr, `"ns":"default"`) 87 assert.Contains(t, logStr, `"ignoredKey":12345`) 88 89 buffer.Reset() 90 testLogger.Info("invalid", zap.String("ns", "default"), 12345, "abcde") 91 err = writer.Flush() 92 require.Nil(t, err) 93 94 logStr = buffer.String() 95 fmt.Println(logStr) 96 assert.Contains(t, logStr, `"level":"dpanic"`) 97 assert.Contains(t, logStr, `"caller":"zlog/logr_test.go:`) 98 assert.Contains(t, logStr, `"msg":"invalid"`) 99 assert.Contains(t, logStr, `"ns":"default"`) 100 assert.Contains(t, logStr, `"invalidKey":12345`) 101 } 102 103 func TestLogrLoggerError(t *testing.T) { 104 for _, logErrKey := range []string{ 105 "err", 106 "error", 107 } { 108 t.Run(fmt.Sprintf("error field name %s", logErrKey), func(t *testing.T) { 109 var buffer bytes.Buffer 110 writer := bufio.NewWriter(&buffer) 111 zl := newTestLogrLogger(InfoLevel, writer) 112 testLogger := NewLogrLogger(func(opts *LogrOptions) { 113 opts.ErrorKey = logErrKey 114 opts.NumericLevelKey = "v" 115 opts.Logger = zl 116 }) 117 118 // Errors always get logged, regardless of log levels. 119 testLogger.V(10).Error(fmt.Errorf("invalid namespace:%s", "default"), "wrong namespace", "ns", "default", "podnum", 2) 120 err := writer.Flush() 121 require.Nil(t, err) 122 123 logStr := buffer.String() 124 assert.Contains(t, logStr, `"level":"error"`) 125 assert.Contains(t, logStr, `"caller":"zlog/logr_test.go:`) 126 assert.Contains(t, logStr, `"msg":"wrong namespace"`) 127 assert.Contains(t, logStr, `"ns":"default"`) 128 assert.Contains(t, logStr, `"podnum":2`) 129 }) 130 } 131 } 132 133 func TestLogrLoggerEnabled(t *testing.T) { 134 for i := 0; i < 11; i++ { 135 t.Run(fmt.Sprintf("logger level %d", i), func(t *testing.T) { 136 testLogger := NewLogrLogger(func(opts *LogrOptions) { 137 opts.Logger = newTestLogrLogger(Level(-i), nil) 138 }) 139 140 for j := 0; j <= 128; j++ { 141 shouldBeEnabled := i >= j 142 t.Run(fmt.Sprintf("message level %d", j), func(t *testing.T) { 143 isEnabled := testLogger.V(j).Enabled() 144 if !isEnabled && shouldBeEnabled { 145 t.Errorf("V(%d).Info should be enabled", j) 146 } else if isEnabled && !shouldBeEnabled { 147 t.Errorf("V(%d).Info should not be enabled", j) 148 } 149 150 log := testLogger 151 for k := 0; k < j; k++ { 152 log = log.V(1) 153 } 154 isEnabled = log.Enabled() 155 if !isEnabled && shouldBeEnabled { 156 t.Errorf("repeated V(1).Info should be enabled") 157 } else if isEnabled && !shouldBeEnabled { 158 t.Errorf("repeated V(1).Info should not be enabled") 159 } 160 }) 161 } 162 }) 163 } 164 } 165 166 func TestLogrNumericLevel(t *testing.T) { 167 for _, logNumKey := range []string{ 168 "", 169 "v", 170 "verbose", 171 } { 172 t.Run(fmt.Sprintf("numeric verbosity field %q", logNumKey), func(t *testing.T) { 173 for i := 0; i < 4; i++ { 174 var buffer bytes.Buffer 175 writer := bufio.NewWriter(&buffer) 176 testLogger := NewLogrLogger(func(opts *LogrOptions) { 177 opts.NumericLevelKey = logNumKey 178 opts.Logger = newTestLogrLogger(Level(-100), writer) 179 }) 180 181 testLogger.V(i).Info("test", "ns", "default", "podnum", 2) 182 err := writer.Flush() 183 require.Nil(t, err) 184 185 logStr := buffer.String() 186 fmt.Println(logStr) 187 188 assert.Contains(t, logStr, `"caller":"zlog/logr_test.go:`) 189 if logNumKey != "" { 190 assert.Contains(t, logStr, fmt.Sprintf(`"%s":%d`, logNumKey, i)) 191 } 192 } 193 }) 194 } 195 }