| 440 | } |
| 441 | |
| 442 | func TestSugarPanicLogging(t *testing.T) { |
| 443 | tests := []struct { |
| 444 | loggerLevel zapcore.Level |
| 445 | f func(*SugaredLogger) |
| 446 | expectedMsg string |
| 447 | }{ |
| 448 | {FatalLevel, func(s *SugaredLogger) { s.Panic("foo") }, ""}, |
| 449 | {PanicLevel, func(s *SugaredLogger) { s.Panic("foo") }, "foo"}, |
| 450 | {DebugLevel, func(s *SugaredLogger) { s.Panic("foo") }, "foo"}, |
| 451 | {FatalLevel, func(s *SugaredLogger) { s.Panicf("%s", "foo") }, ""}, |
| 452 | {PanicLevel, func(s *SugaredLogger) { s.Panicf("%s", "foo") }, "foo"}, |
| 453 | {DebugLevel, func(s *SugaredLogger) { s.Panicf("%s", "foo") }, "foo"}, |
| 454 | {FatalLevel, func(s *SugaredLogger) { s.Panicw("foo") }, ""}, |
| 455 | {PanicLevel, func(s *SugaredLogger) { s.Panicw("foo") }, "foo"}, |
| 456 | {DebugLevel, func(s *SugaredLogger) { s.Panicw("foo") }, "foo"}, |
| 457 | {FatalLevel, func(s *SugaredLogger) { s.Panicln("foo") }, ""}, |
| 458 | {PanicLevel, func(s *SugaredLogger) { s.Panicln("foo") }, "foo"}, |
| 459 | {DebugLevel, func(s *SugaredLogger) { s.Panicln("foo") }, "foo"}, |
| 460 | } |
| 461 | |
| 462 | for _, tt := range tests { |
| 463 | withSugar(t, tt.loggerLevel, nil, func(sugar *SugaredLogger, logs *observer.ObservedLogs) { |
| 464 | assert.Panics(t, func() { tt.f(sugar) }, "Expected panic-level logger calls to panic.") |
| 465 | if tt.expectedMsg != "" { |
| 466 | assert.Equal(t, []observer.LoggedEntry{{ |
| 467 | Context: []Field{}, |
| 468 | Entry: zapcore.Entry{Message: tt.expectedMsg, Level: PanicLevel}, |
| 469 | }}, logs.AllUntimed(), "Unexpected log output.") |
| 470 | } else { |
| 471 | assert.Equal(t, 0, logs.Len(), "Didn't expect any log output.") |
| 472 | } |
| 473 | }) |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func TestSugarFatalLogging(t *testing.T) { |
| 478 | tests := []struct { |