github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/xlog/console_core_test.go (about) 1 package xlog 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 "go.uber.org/zap" 9 "go.uber.org/zap/zapcore" 10 ) 11 12 func TestConsoleCore(t *testing.T) { 13 lvlEnabler := zap.NewAtomicLevelAt(LogLevelDebug.zapLevel()) 14 var cc xLogCore = newConsoleCore( 15 nil, 16 &lvlEnabler, 17 JSON, 18 zapcore.CapitalLevelEncoder, 19 zapcore.ISO8601TimeEncoder, 20 ) 21 require.Nil(t, cc) 22 23 cc = newConsoleCore( 24 context.TODO(), 25 &lvlEnabler, 26 JSON, 27 zapcore.CapitalLevelEncoder, 28 zapcore.ISO8601TimeEncoder, 29 ) 30 require.NotNil(t, cc.outEncoder()) 31 require.NotNil(t, cc.writeSyncer()) 32 require.NotNil(t, cc.levelEncoder()) 33 require.NotNil(t, cc.timeEncoder()) 34 require.NotNil(t, cc.(*consoleCore).core.lvlEnabler) 35 require.NotNil(t, cc.(*consoleCore).core.core) 36 37 require.True(t, cc.Enabled(zapcore.DebugLevel)) 38 require.True(t, cc.Enabled(zapcore.InfoLevel)) 39 require.True(t, cc.Enabled(zapcore.WarnLevel)) 40 require.True(t, cc.Enabled(zapcore.ErrorLevel)) 41 42 lvlEnabler.SetLevel(zapcore.ErrorLevel) 43 require.False(t, cc.Enabled(zapcore.DebugLevel)) 44 require.False(t, cc.Enabled(zapcore.InfoLevel)) 45 require.False(t, cc.Enabled(zapcore.WarnLevel)) 46 require.True(t, cc.Enabled(zapcore.ErrorLevel)) 47 48 lvlEnabler.SetLevel(zapcore.DebugLevel) 49 50 core := cc.With([]zap.Field{zap.String("key", "value")}) 51 require.NotNil(t, core) 52 53 ent := cc.Check(zapcore.Entry{Level: zapcore.DebugLevel}, nil) 54 err := cc.Write(ent.Entry, []zap.Field{zap.String("key", "value")}) 55 require.NoError(t, err) 56 _ = cc.Sync() 57 58 cc, err = WrapCore(cc, componentCoreEncoderCfg()) 59 require.NoError(t, err) 60 require.NotNil(t, cc) 61 err = cc.Write(zapcore.Entry{Level: zapcore.DebugLevel, LoggerName: "commonCore"}, []zap.Field{zap.String("key", "value")}) 62 require.NoError(t, err) 63 _ = cc.Sync() 64 }