github.com/keakon/golog@v0.0.0-20230330091222-cac71197c18d/log/log_test.go (about) 1 //go:build !race 2 // +build !race 3 4 // golog.FastTimer is not thread-safe. 5 6 package log 7 8 import ( 9 "bytes" 10 "errors" 11 "os" 12 "path/filepath" 13 "testing" 14 15 "github.com/keakon/golog" 16 ) 17 18 type memoryWriter struct { 19 bytes.Buffer 20 } 21 22 func (w *memoryWriter) Close() error { 23 w.Buffer.Reset() 24 return nil 25 } 26 27 func errorFunc(args ...interface{}) { 28 if len(args) == 1 { 29 arg := args[0] 30 if _, ok := arg.(error); ok { 31 // skip 32 return 33 } 34 } 35 file, line := golog.Caller(1) 36 defaultLogger.Log(golog.ErrorLevel, file, line, "", args...) 37 } 38 39 func errorfFunc(msg string, args ...interface{}) { 40 if len(args) == 1 { 41 arg := args[0] 42 if _, ok := arg.(error); ok { 43 // skip 44 return 45 } 46 } 47 file, line := golog.Caller(1) 48 defaultLogger.Log(golog.ErrorLevel, file, line, msg, args...) 49 } 50 51 func TestSetLogFunc(t *testing.T) { 52 golog.StartFastTimer() 53 defer golog.StopFastTimer() 54 55 e := errors.New("test") 56 w := &memoryWriter{} 57 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 58 h.AddWriter(w) 59 l := golog.NewLogger(golog.InfoLevel) 60 l.AddHandler(h) 61 62 SetDefaultLogger(l) 63 SetLogFunc(errorFunc, golog.ErrorLevel) 64 SetLogfFunc(errorfFunc, golog.ErrorLevel) 65 66 Debug("test") 67 if w.Buffer.Len() != 0 { 68 t.Error("memoryWriter is not empty") 69 w.Buffer.Reset() 70 } 71 72 Error(e) 73 if w.Buffer.Len() != 0 { 74 t.Error("memoryWriter is not empty") 75 w.Buffer.Reset() 76 } 77 78 Error("test") 79 if w.Buffer.Len() == 0 { 80 t.Error("memoryWriter is empty") 81 } 82 w.Buffer.Reset() 83 84 Errorf("error: %v", e) 85 if w.Buffer.Len() != 0 { 86 t.Error("memoryWriter is not empty") 87 w.Buffer.Reset() 88 } 89 90 Errorf("error: %s", "test") 91 if w.Buffer.Len() == 0 { 92 t.Error("memoryWriter is empty") 93 } 94 w.Buffer.Reset() 95 96 SetLogFunc(errorFunc, golog.DebugLevel) 97 SetLogfFunc(errorfFunc, golog.DebugLevel) 98 99 for level := golog.DebugLevel; level <= golog.CritLevel; level++ { 100 SetLogFunc(errorFunc, level) 101 SetLogfFunc(errorfFunc, level) 102 } 103 104 Debug(e) 105 if w.Buffer.Len() != 0 { 106 t.Error("memoryWriter is not empty") 107 w.Buffer.Reset() 108 } 109 110 Debug("test") 111 if w.Buffer.Len() == 0 { 112 t.Error("memoryWriter is empty") 113 } 114 w.Buffer.Reset() 115 116 Debugf("error: %v", e) 117 if w.Buffer.Len() != 0 { 118 t.Error("memoryWriter is not empty") 119 w.Buffer.Reset() 120 } 121 122 Debugf("error: %s", "test") 123 if w.Buffer.Len() == 0 { 124 t.Error("memoryWriter is empty") 125 } 126 w.Buffer.Reset() 127 128 Info(e) 129 if w.Buffer.Len() != 0 { 130 t.Error("memoryWriter is not empty") 131 w.Buffer.Reset() 132 } 133 134 Info("test") 135 if w.Buffer.Len() == 0 { 136 t.Error("memoryWriter is empty") 137 } 138 w.Buffer.Reset() 139 140 Infof("error: %v", e) 141 if w.Buffer.Len() != 0 { 142 t.Error("memoryWriter is not empty") 143 w.Buffer.Reset() 144 } 145 146 Infof("error: %s", "test") 147 if w.Buffer.Len() == 0 { 148 t.Error("memoryWriter is empty") 149 } 150 w.Buffer.Reset() 151 152 Crit(e) 153 if w.Buffer.Len() != 0 { 154 t.Error("memoryWriter is not empty") 155 w.Buffer.Reset() 156 } 157 158 Crit("test") 159 if w.Buffer.Len() == 0 { 160 t.Error("memoryWriter is empty") 161 } 162 w.Buffer.Reset() 163 164 Critf("error: %v", e) 165 if w.Buffer.Len() != 0 { 166 t.Error("memoryWriter is not empty") 167 w.Buffer.Reset() 168 } 169 170 Critf("error: %s", "test") 171 if w.Buffer.Len() == 0 { 172 t.Error("memoryWriter is empty") 173 } 174 w.Buffer.Reset() 175 176 l.Close() 177 } 178 179 func TestLogFuncs(t *testing.T) { 180 golog.StartFastTimer() 181 defer golog.StopFastTimer() 182 183 w := &memoryWriter{} 184 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 185 h.AddWriter(w) 186 l := golog.NewLogger(golog.InfoLevel) 187 l.AddHandler(h) 188 SetDefaultLogger(l) 189 190 Debug("test") 191 if w.Buffer.Len() != 0 { 192 t.Error("memoryWriter is not empty") 193 } 194 Debugf("test") 195 if w.Buffer.Len() != 0 { 196 t.Error("memoryWriter is not empty") 197 } 198 199 Info("test") 200 if w.Buffer.Len() == 0 { 201 t.Error("memoryWriter is empty") 202 } 203 w.Buffer.Reset() 204 205 Infof("test") 206 if w.Buffer.Len() == 0 { 207 t.Error("memoryWriter is empty") 208 } 209 w.Buffer.Reset() 210 211 Warn("test") 212 if w.Buffer.Len() == 0 { 213 t.Error("memoryWriter is empty") 214 } 215 w.Buffer.Reset() 216 217 Warnf("test") 218 if w.Buffer.Len() == 0 { 219 t.Error("memoryWriter is empty") 220 } 221 w.Buffer.Reset() 222 223 Error("test") 224 if w.Buffer.Len() == 0 { 225 t.Error("memoryWriter is empty") 226 } 227 w.Buffer.Reset() 228 229 Errorf("test") 230 if w.Buffer.Len() == 0 { 231 t.Error("memoryWriter is empty") 232 } 233 234 Crit("test") 235 if w.Buffer.Len() == 0 { 236 t.Error("memoryWriter is empty") 237 } 238 w.Buffer.Reset() 239 240 Critf("test") 241 if w.Buffer.Len() == 0 { 242 t.Error("memoryWriter is empty") 243 } 244 l.Close() 245 246 h = golog.NewHandler(golog.ErrorLevel, golog.DefaultFormatter) 247 h.AddWriter(w) 248 l = golog.NewLogger(golog.ErrorLevel) 249 l.AddHandler(h) 250 SetDefaultLogger(l) 251 252 Info("test") 253 if w.Buffer.Len() != 0 { 254 t.Error("memoryWriter is not empty") 255 } 256 w.Buffer.Reset() 257 258 Error("test") 259 if w.Buffer.Len() == 0 { 260 t.Error("memoryWriter is empty") 261 } 262 l.Close() 263 } 264 265 func TestIsEnabledFor(t *testing.T) { 266 SetDefaultLogger(nil) 267 if IsEnabledFor(golog.DebugLevel) { 268 t.Error("nil logger is enabled for debug level") 269 } 270 271 l := golog.NewStdoutLogger() 272 SetDefaultLogger(l) 273 if IsEnabledFor(golog.DebugLevel) { 274 t.Error("info logger is enabled for debug level") 275 } 276 if !IsEnabledFor(golog.InfoLevel) { 277 t.Error("info logger is disabled for info level") 278 } 279 if !IsEnabledFor(golog.ErrorLevel) { 280 t.Error("info logger is disabled for error level") 281 } 282 } 283 284 func BenchmarkDiscardLogger(b *testing.B) { 285 golog.StartFastTimer() 286 defer golog.StopFastTimer() 287 288 w := golog.NewDiscardWriter() 289 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 290 h.AddWriter(w) 291 l := golog.NewLogger(golog.InfoLevel) 292 l.AddHandler(h) 293 SetDefaultLogger(l) 294 295 b.ResetTimer() 296 297 for i := 0; i < b.N; i++ { 298 Infof("test") 299 } 300 l.Close() 301 } 302 303 func BenchmarkDiscardLoggerParallel(b *testing.B) { 304 golog.StartFastTimer() 305 defer golog.StopFastTimer() 306 307 w := golog.NewDiscardWriter() 308 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 309 h.AddWriter(w) 310 l := golog.NewLogger(golog.InfoLevel) 311 l.AddHandler(h) 312 SetDefaultLogger(l) 313 314 b.ResetTimer() 315 316 b.RunParallel(func(pb *testing.PB) { 317 for pb.Next() { 318 Infof("test") 319 } 320 }) 321 l.Close() 322 } 323 324 func BenchmarkDiscardLoggerWithoutTimer(b *testing.B) { 325 w := golog.NewDiscardWriter() 326 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 327 h.AddWriter(w) 328 l := golog.NewLogger(golog.InfoLevel) 329 l.AddHandler(h) 330 SetDefaultLogger(l) 331 332 b.ResetTimer() 333 334 for i := 0; i < b.N; i++ { 335 Infof("test") 336 } 337 l.Close() 338 } 339 340 func BenchmarkDiscardLoggerWithoutTimerParallel(b *testing.B) { 341 w := golog.NewDiscardWriter() 342 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 343 h.AddWriter(w) 344 l := golog.NewLogger(golog.InfoLevel) 345 l.AddHandler(h) 346 SetDefaultLogger(l) 347 348 b.ResetTimer() 349 350 b.RunParallel(func(pb *testing.PB) { 351 for pb.Next() { 352 Infof("test") 353 } 354 }) 355 l.Close() 356 } 357 358 func BenchmarkNopLog(b *testing.B) { 359 w := golog.NewDiscardWriter() 360 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 361 h.AddWriter(w) 362 l := golog.NewLogger(golog.InfoLevel) 363 l.AddHandler(h) 364 SetDefaultLogger(l) 365 366 b.ResetTimer() 367 368 for i := 0; i < b.N; i++ { 369 Debugf("test") 370 } 371 l.Close() 372 } 373 374 func BenchmarkNopLogParallel(b *testing.B) { 375 w := golog.NewDiscardWriter() 376 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 377 h.AddWriter(w) 378 l := golog.NewLogger(golog.InfoLevel) 379 l.AddHandler(h) 380 SetDefaultLogger(l) 381 382 b.ResetTimer() 383 384 b.RunParallel(func(pb *testing.PB) { 385 for pb.Next() { 386 Debugf("test") 387 } 388 }) 389 l.Close() 390 } 391 392 func BenchmarkMultiLevels(b *testing.B) { 393 golog.StartFastTimer() 394 defer golog.StopFastTimer() 395 396 w := golog.NewDiscardWriter() 397 dh := golog.NewHandler(golog.DebugLevel, golog.DefaultFormatter) 398 dh.AddWriter(w) 399 ih := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 400 ih.AddWriter(w) 401 wh := golog.NewHandler(golog.WarnLevel, golog.DefaultFormatter) 402 wh.AddWriter(w) 403 eh := golog.NewHandler(golog.ErrorLevel, golog.DefaultFormatter) 404 eh.AddWriter(w) 405 ch := golog.NewHandler(golog.CritLevel, golog.DefaultFormatter) 406 ch.AddWriter(w) 407 408 l := golog.NewLogger(golog.WarnLevel) 409 l.AddHandler(dh) 410 l.AddHandler(ih) 411 l.AddHandler(wh) 412 l.AddHandler(eh) 413 l.AddHandler(ch) 414 SetDefaultLogger(l) 415 416 b.ResetTimer() 417 418 for i := 0; i < b.N; i++ { 419 Debugf("test") 420 Infof("test") 421 Warnf("test") 422 Errorf("test") 423 Critf("test") 424 } 425 l.Close() 426 } 427 428 func BenchmarkMultiLevelsParallel(b *testing.B) { 429 golog.StartFastTimer() 430 defer golog.StopFastTimer() 431 432 w := golog.NewDiscardWriter() 433 dh := golog.NewHandler(golog.DebugLevel, golog.DefaultFormatter) 434 dh.AddWriter(w) 435 ih := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 436 ih.AddWriter(w) 437 wh := golog.NewHandler(golog.WarnLevel, golog.DefaultFormatter) 438 wh.AddWriter(w) 439 eh := golog.NewHandler(golog.ErrorLevel, golog.DefaultFormatter) 440 eh.AddWriter(w) 441 ch := golog.NewHandler(golog.CritLevel, golog.DefaultFormatter) 442 ch.AddWriter(w) 443 444 l := golog.NewLogger(golog.WarnLevel) 445 l.AddHandler(dh) 446 l.AddHandler(ih) 447 l.AddHandler(wh) 448 l.AddHandler(eh) 449 l.AddHandler(ch) 450 SetDefaultLogger(l) 451 452 b.ResetTimer() 453 454 b.RunParallel(func(pb *testing.PB) { 455 for pb.Next() { 456 Debugf("test") 457 Infof("test") 458 Warnf("test") 459 Errorf("test") 460 Critf("test") 461 } 462 }) 463 l.Close() 464 } 465 466 func BenchmarkBufferedFileLogger(b *testing.B) { 467 golog.StartFastTimer() 468 defer golog.StopFastTimer() 469 470 path := filepath.Join(os.TempDir(), "test.log") 471 os.Remove(path) 472 w, err := golog.NewBufferedFileWriter(path) 473 if err != nil { 474 b.Error(err) 475 } 476 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 477 h.AddWriter(w) 478 l := golog.NewLogger(golog.InfoLevel) 479 l.AddHandler(h) 480 SetDefaultLogger(l) 481 482 b.ResetTimer() 483 484 for i := 0; i < b.N; i++ { 485 Infof("test") 486 } 487 l.Close() 488 } 489 490 func BenchmarkBufferedFileLoggerParallel(b *testing.B) { 491 golog.StartFastTimer() 492 defer golog.StopFastTimer() 493 494 path := filepath.Join(os.TempDir(), "test.log") 495 os.Remove(path) 496 w, err := golog.NewBufferedFileWriter(path) 497 if err != nil { 498 b.Error(err) 499 } 500 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 501 h.AddWriter(w) 502 l := golog.NewLogger(golog.InfoLevel) 503 l.AddHandler(h) 504 SetDefaultLogger(l) 505 506 b.ResetTimer() 507 508 b.RunParallel(func(pb *testing.PB) { 509 for pb.Next() { 510 Infof("test") 511 } 512 }) 513 l.Close() 514 } 515 516 func BenchmarkConcurrentFileLogger(b *testing.B) { 517 golog.StartFastTimer() 518 defer golog.StopFastTimer() 519 520 path := filepath.Join(os.TempDir(), "test.log") 521 os.Remove(path) 522 w, err := golog.NewConcurrentFileWriter(path, golog.BufferSize(1024*1024*8)) 523 if err != nil { 524 b.Error(err) 525 } 526 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 527 h.AddWriter(w) 528 l := golog.NewLogger(golog.InfoLevel) 529 l.AddHandler(h) 530 SetDefaultLogger(l) 531 532 b.ResetTimer() 533 534 for i := 0; i < b.N; i++ { 535 Infof("test") 536 } 537 l.Close() 538 } 539 540 func BenchmarkConcurrentFileLoggerParallel(b *testing.B) { 541 golog.StartFastTimer() 542 defer golog.StopFastTimer() 543 544 path := filepath.Join(os.TempDir(), "test.log") 545 os.Remove(path) 546 w, err := golog.NewConcurrentFileWriter(path, golog.BufferSize(1024*1024*8)) 547 if err != nil { 548 b.Error(err) 549 } 550 h := golog.NewHandler(golog.InfoLevel, golog.DefaultFormatter) 551 h.AddWriter(w) 552 l := golog.NewLogger(golog.InfoLevel) 553 l.AddHandler(h) 554 SetDefaultLogger(l) 555 556 b.ResetTimer() 557 558 b.RunParallel(func(pb *testing.PB) { 559 for pb.Next() { 560 Infof("test") 561 } 562 }) 563 l.Close() 564 }