MCPcopy
hub / github.com/rs/zerolog / TestFatalLevel

Function TestFatalLevel

log_test.go:996–1032  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

994}
995
996func TestFatalLevel(t *testing.T) {
997 lw := &levelWriter{
998 ops: []struct {
999 l Level
1000 p string
1001 }{},
1002 }
1003
1004 // Allow extra-verbose logs.
1005 SetGlobalLevel(TraceLevel - 1)
1006 log := New(lw).Level(TraceLevel - 1)
1007
1008 // Set FatalExitFunc to panic so we can catch it
1009 oldFatalExitFunc := FatalExitFunc
1010 FatalExitFunc = func() { panic("fatal exit") }
1011 defer func() { FatalExitFunc = oldFatalExitFunc }()
1012
1013 // Catch the panic from log.Fatal().Msg("1")
1014 defer func() {
1015 if r := recover(); r == nil || r != "fatal exit" {
1016 t.Errorf("expected panic 'fatal exit' from log.Fatal(), got %v", r)
1017 }
1018 }()
1019 log.Fatal().Msg("1")
1020 log.WithLevel(FatalLevel).Msg("2")
1021
1022 want := []struct {
1023 l Level
1024 p string
1025 }{
1026 {FatalLevel, `{"level":"fatal","message":"1"}` + "\n"},
1027 {FatalLevel, `{"level":"fatal","message":"2"}` + "\n"},
1028 }
1029 if got := lw.ops; !reflect.DeepEqual(got, want) {
1030 t.Errorf("invalid ops:\ngot:\n%v\nwant:\n%v", got, want)
1031 }
1032}
1033
1034func TestFatalDisabled(t *testing.T) {
1035 out := &bytes.Buffer{}

Callers

nothing calls this directly

Calls 6

SetGlobalLevelFunction · 0.85
NewFunction · 0.85
LevelMethod · 0.80
MsgMethod · 0.80
FatalMethod · 0.80
WithLevelMethod · 0.80

Tested by

no test coverage detected