package logger import ( "fmt" "sync" "testing" ) // TestLogHookReceivesEntries replaces the old in-memory buffer tests: the // buffer (and its GetLogs accessor) was removed with the web log viewer, and // the hook is now the only side channel tests assert on. func TestLogHookReceivesEntries(t *testing.T) { var mu sync.Mutex var got []string prev := SetLogHook(func(level, msg string) { mu.Lock() defer mu.Unlock() got = append(got, level+" "+msg) }) t.Cleanup(func() { SetLogHook(prev) }) Debug("d1") Infof("i%d", 2) Warning("w3") Errorf("e%s", "4") want := []string{"DEBUG d1", "INFO i2", "WARNING w3", "ERROR e4"} mu.Lock() defer mu.Unlock() if len(got) != len(want) { t.Fatalf("hook captured %v, want %v", got, want) } for i := range want { if got[i] != want[i] { t.Fatalf("hook entry %d = %q, want %q", i, got[i], want[i]) } } } // TestLogHookUnsetIsSilent guards the production path: with no hook // installed, logging must simply not panic. func TestLogHookUnsetIsSilent(t *testing.T) { prev := SetLogHook(nil) t.Cleanup(func() { SetLogHook(prev) }) for i := range 3 { Info(fmt.Sprintf("m%d", i)) } }