github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/xlog/ants_test.go (about) 1 package xlog 2 3 import ( 4 "testing" 5 "time" 6 7 antsv2 "github.com/panjf2000/ants/v2" 8 "github.com/stretchr/testify/require" 9 "go.uber.org/zap/zapcore" 10 ) 11 12 func TestAntsXLogger_ParentLogLevelChanged(t *testing.T) { 13 var ( 14 parentLogger XLogger = nil 15 logger *AntsXLogger = nil 16 ) 17 logger.Printf("test %d", 123) 18 19 opts := []XLoggerOption{ 20 WithXLoggerLevel(LogLevelDebug), 21 WithXLoggerEncoder(JSON), 22 WithXLoggerTimeEncoder(zapcore.ISO8601TimeEncoder), 23 WithXLoggerLevelEncoder(zapcore.CapitalLevelEncoder), 24 } 25 parentLogger = NewXLogger(opts...) 26 logger = NewAntsXLogger(parentLogger) 27 parentLogger.IncreaseLogLevel(zapcore.InfoLevel) 28 parentLogger.Debug("abc") 29 logger.Printf("test %d", 123) 30 parentLogger.IncreaseLogLevel(zapcore.DebugLevel) 31 parentLogger.Debug("abc") 32 logger.Printf("test %d", 123) 33 _ = parentLogger.Sync() 34 } 35 36 func TestAntsXLogger_AntsPool(t *testing.T) { 37 var ( 38 parentLogger XLogger = nil 39 logger *AntsXLogger = nil 40 ) 41 opts := []XLoggerOption{ 42 WithXLoggerLevel(LogLevelDebug), 43 WithXLoggerEncoder(JSON), 44 WithXLoggerTimeEncoder(zapcore.ISO8601TimeEncoder), 45 WithXLoggerLevelEncoder(zapcore.CapitalLevelEncoder), 46 } 47 parentLogger = NewXLogger(opts...) 48 logger = NewAntsXLogger(parentLogger) 49 50 p, err := antsv2.NewPool(10, antsv2.WithLogger(logger)) 51 require.NoError(t, err) 52 err = p.Submit(func() { 53 parentLogger.Logf(LogLevelDebug.zapLevel(), "test %d", 123) 54 }) 55 require.NoError(t, err) 56 err = p.Submit(func() { 57 panic("xlogger panic in ants pool") 58 }) 59 time.Sleep(100 * time.Millisecond) 60 _ = parentLogger.Sync() 61 }