github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/xlog/fx_test.go (about) 1 package xlog 2 3 import ( 4 "errors" 5 "os" 6 "testing" 7 8 "go.uber.org/fx/fxevent" 9 "go.uber.org/zap/zapcore" 10 ) 11 12 func TestFxXLoggerAllCases(t *testing.T) { 13 testcases := []struct { 14 name string 15 event fxevent.Event 16 }{ 17 { 18 "onStartExecuting", 19 &fxevent.OnStartExecuting{ 20 FunctionName: "testFunc1", 21 CallerName: "testCaller1", 22 }, 23 }, 24 { 25 "onStopExecuting", 26 &fxevent.OnStartExecuting{ 27 FunctionName: "testFunc4", 28 CallerName: "testCaller4", 29 }, 30 }, 31 { 32 "onStopExecuted_err", 33 &fxevent.OnStartExecuted{ 34 FunctionName: "testFunc5", 35 CallerName: "testCaller5", 36 Runtime: 11, 37 Err: errors.New("fx error 2"), 38 }, 39 }, 40 { 41 "onStopExecuted_succ", 42 &fxevent.OnStartExecuted{ 43 FunctionName: "testFunc6", 44 CallerName: "testCaller6", 45 Runtime: 12, 46 }, 47 }, 48 { 49 "supplied_err", 50 &fxevent.Supplied{ 51 TypeName: "testType1", 52 Err: errors.New("fx error 3"), 53 StackTrace: []string{"testStack1"}, 54 }, 55 }, 56 { 57 "supplied_succ1", 58 &fxevent.Supplied{ 59 TypeName: "testType2", 60 ModuleName: "testModule1", 61 }, 62 }, 63 { 64 "supplied_succ2", 65 &fxevent.Supplied{ 66 TypeName: "testType3", 67 }, 68 }, 69 { 70 "provided_err", 71 &fxevent.Provided{ 72 ConstructorName: "testConstructor1", 73 Err: errors.New("fx error 4"), 74 StackTrace: []string{"testStack2"}, 75 }, 76 }, 77 { 78 "provided_succ1", 79 &fxevent.Provided{ 80 OutputTypeNames: []string{"testType4"}, 81 ConstructorName: "testConstructor2", 82 ModuleName: "testModule2", 83 }, 84 }, 85 { 86 "provided_succ2", 87 &fxevent.Provided{ 88 OutputTypeNames: []string{"testType5"}, 89 ConstructorName: "testConstructor3", 90 Private: true, 91 }, 92 }, 93 { 94 "replaced_err", 95 &fxevent.Replaced{ 96 OutputTypeNames: []string{"testType4"}, 97 Err: errors.New("fx error 5"), 98 StackTrace: []string{"testStack3"}, 99 }, 100 }, 101 { 102 "replaced_succ1", 103 &fxevent.Replaced{ 104 OutputTypeNames: []string{"testType5"}, 105 ModuleName: "testModule3", 106 }, 107 }, 108 { 109 "replaced_succ2", 110 &fxevent.Replaced{ 111 OutputTypeNames: []string{"testType6"}, 112 }, 113 }, 114 { 115 "decorated_err", 116 &fxevent.Decorated{ 117 DecoratorName: "testDecorator1", 118 Err: errors.New("fx error 6"), 119 StackTrace: []string{"testStack4"}, 120 }, 121 }, 122 { 123 "decorated_succ1", 124 &fxevent.Decorated{ 125 OutputTypeNames: []string{"testType7"}, 126 DecoratorName: "testDecorator2", 127 ModuleName: "testModule4", 128 }, 129 }, 130 { 131 "decorated_succ2", 132 &fxevent.Decorated{ 133 OutputTypeNames: []string{"testType8"}, 134 DecoratorName: "testDecorator3", 135 }, 136 }, 137 { 138 "invoked_err", 139 &fxevent.Invoked{ 140 FunctionName: "testFunc7", 141 Err: errors.New("fx error 7"), 142 Trace: "invoke trace 1", 143 }, 144 }, 145 { 146 "invoking_succ1", 147 &fxevent.Invoking{ 148 FunctionName: "testFunc8", 149 ModuleName: "testModule5", 150 }, 151 }, 152 { 153 "invoking_succ2", 154 &fxevent.Invoking{ 155 FunctionName: "testFunc9", 156 }, 157 }, 158 { 159 "stopping", 160 &fxevent.Stopping{ 161 Signal: os.Kill, 162 }, 163 }, 164 { 165 "stopped", 166 &fxevent.Stopped{ 167 Err: errors.New("fx error 8"), 168 }, 169 }, 170 { 171 "rollingBack", 172 &fxevent.RollingBack{ 173 StartErr: errors.New("fx error 9"), 174 }, 175 }, 176 { 177 "rolledBack", 178 &fxevent.RolledBack{ 179 Err: errors.New("fx error 10"), 180 }, 181 }, 182 { 183 "started_err", 184 &fxevent.Started{ 185 Err: errors.New("fx error 11"), 186 }, 187 }, 188 { 189 "started_succ", 190 &fxevent.Started{}, 191 }, 192 { 193 "loggerInitialized_err", 194 &fxevent.LoggerInitialized{ 195 Err: errors.New("fx error 12"), 196 }, 197 }, 198 { 199 "loggerInitialized_succ", 200 &fxevent.LoggerInitialized{ 201 ConstructorName: "testConstructor4", 202 }, 203 }, 204 { 205 "onStopExecuted_err", 206 &fxevent.OnStopExecuted{ 207 FunctionName: "testFunc10", 208 CallerName: "testCaller7", 209 Runtime: 13, 210 Err: errors.New("fx error 13"), 211 }, 212 }, 213 { 214 "onStopExecuted_succ", 215 &fxevent.OnStopExecuted{ 216 FunctionName: "testFunc11", 217 CallerName: "testCaller8", 218 Runtime: 14, 219 }, 220 }, 221 { 222 "onStopExecuting", 223 &fxevent.OnStopExecuting{ 224 FunctionName: "testFunc12", 225 CallerName: "testCaller9", 226 }, 227 }, 228 } 229 opts := []XLoggerOption{ 230 WithXLoggerLevel(LogLevelDebug), 231 WithXLoggerEncoder(JSON), 232 WithXLoggerTimeEncoder(zapcore.ISO8601TimeEncoder), 233 WithXLoggerLevelEncoder(zapcore.CapitalLevelEncoder), 234 } 235 logger := NewFxXLogger(NewXLogger(opts...)) 236 for _, tc := range testcases { 237 t.Run(tc.name, func(t *testing.T) { 238 logger.LogEvent(tc.event) 239 _ = logger.logger.Sync() 240 }) 241 } 242 } 243 244 func TestFxXLogger_ParentLogLevelChanged(t *testing.T) { 245 var ( 246 parentLogger XLogger = nil 247 logger *FxXLogger = nil 248 ) 249 logger.LogEvent(&fxevent.LoggerInitialized{ 250 ConstructorName: "testConstructor4", 251 }) 252 253 opts := []XLoggerOption{ 254 WithXLoggerLevel(LogLevelDebug), 255 WithXLoggerEncoder(JSON), 256 WithXLoggerTimeEncoder(zapcore.ISO8601TimeEncoder), 257 WithXLoggerLevelEncoder(zapcore.CapitalLevelEncoder), 258 } 259 parentLogger = NewXLogger(opts...) 260 logger = NewFxXLogger(parentLogger) 261 parentLogger.IncreaseLogLevel(zapcore.InfoLevel) 262 parentLogger.Debug("abc") 263 logger.LogEvent(&fxevent.LoggerInitialized{ 264 ConstructorName: "testConstructor4", 265 }) 266 parentLogger.IncreaseLogLevel(zapcore.DebugLevel) 267 parentLogger.Debug("abc") 268 logger.LogEvent(&fxevent.LoggerInitialized{ 269 ConstructorName: "testConstructor4", 270 }) 271 _ = parentLogger.Sync() 272 }