github.com/matrixorigin/matrixone@v0.7.0/pkg/logutil/report_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package logutil 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 "go.uber.org/zap" 23 "go.uber.org/zap/zapcore" 24 ) 25 26 func TestNoop(t *testing.T) { 27 require.Equal(t, zap.String("span", "{}"), noopContextField(context.Background())) 28 buf, err := noopReportZap(nil, zapcore.Entry{}, nil) 29 require.Equal(t, nil, err) 30 require.Equal(t, "", buf.String()) 31 require.Equal(t, zap.Bool(MOInternalFiledKeyNoopReport, true), NoReportFiled()) 32 } 33 34 func TestReport(t *testing.T) { 35 type fields struct { 36 Level string 37 Format string 38 Filename string 39 MaxSize int 40 MaxDays int 41 MaxBackups int 42 43 Entry zapcore.Entry 44 } 45 tests := []struct { 46 name string 47 fields fields 48 wantLevel zap.AtomicLevel 49 wantOpts []zap.Option 50 wantSyncer zapcore.WriteSyncer 51 wantEncoder zapcore.Encoder 52 wantSinksLen int 53 }{ 54 { 55 name: "normal", 56 fields: fields{ 57 Level: "debug", 58 Format: "console", 59 Filename: "", 60 MaxSize: 0, 61 MaxDays: 0, 62 MaxBackups: 0, 63 64 Entry: zapcore.Entry{Level: zapcore.DebugLevel, Message: "console msg"}, 65 }, 66 wantLevel: zap.NewAtomicLevelAt(zap.DebugLevel), 67 wantOpts: []zap.Option{zap.AddStacktrace(zapcore.FatalLevel), zap.AddCaller()}, 68 wantSyncer: getConsoleSyncer(), 69 wantEncoder: getLoggerEncoder("console"), 70 wantSinksLen: 2, 71 }, 72 } 73 74 //zapReporter.Store(reportZapFunc(func(encoder zapcore.Encoder, entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { 75 //})) 76 for _, tt := range tests { 77 78 t.Run(tt.name, func(t *testing.T) { 79 cfg := &LogConfig{ 80 Level: tt.fields.Level, 81 Format: tt.fields.Format, 82 Filename: tt.fields.Filename, 83 MaxSize: tt.fields.MaxSize, 84 MaxDays: tt.fields.MaxDays, 85 MaxBackups: tt.fields.MaxBackups, 86 } 87 require.Equal(t, tt.wantLevel, cfg.getLevel()) 88 require.Equal(t, len(tt.wantOpts), len(cfg.getOptions())) 89 require.Equal(t, tt.wantSyncer, cfg.getSyncer()) 90 wantMsg, _ := tt.wantEncoder.EncodeEntry(tt.fields.Entry, nil) 91 gotMsg, _ := cfg.getEncoder().EncodeEntry(tt.fields.Entry, nil) 92 require.Equal(t, wantMsg.String(), gotMsg.String()) 93 require.Equal(t, 2, len(cfg.getSinks())) 94 SetupMOLogger(cfg) 95 96 gotCfg := getGlobalLogConfig() 97 require.Equal(t, cfg.DisableStore, gotCfg.DisableStore) 98 99 Info("hello", GetContextFieldFunc()(context.Background()), zap.Int("int", 0)) 100 Info("hello, noop", NoReportFiled()) 101 102 }) 103 } 104 }