- River logs as component river at warn, conga at log.level, -v lets River info through - an app-published logger wins for River and conga - GORM: warn by default, info traces at -vvv, record-not-found ignored, parameterized SQL - gate: the diary, sunscreen, conga and lagoon names in the go stage
92 lines
3.3 KiB
Go
92 lines
3.3 KiB
Go
package conga
|
|
|
|
import (
|
|
"bytes"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/bonfire"
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
"git.golem15.com/golem15/summercms/modules/diary"
|
|
)
|
|
|
|
// TestRiverLoggerComponent: River logs as component river, which the
|
|
// framework defaults hold at warn while conga's own lines (component
|
|
// conga) follow log.level; verbosity 1 lets River's info through.
|
|
func TestRiverLoggerComponent(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
t.Cleanup(diary.Reset)
|
|
cfg, err := compass.Open(compass.Options{Dir: t.TempDir(), Environ: []string{"SUMMER_LOG__DEFAULT=console"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := diary.Setup(cfg, diary.Options{Stderr: &buf, Color: bonfire.ColorNever, Root: t.TempDir(), Environ: []string{}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
riverLogger(nil).Info("river info at v0")
|
|
riverLogger(nil).Warn("river warn at v0")
|
|
loggerFromApp(nil).Info("conga info at v0")
|
|
out := buf.String()
|
|
if strings.Contains(out, "river info at v0") {
|
|
t.Errorf("River info passed its warn floor:\n%s", out)
|
|
}
|
|
for _, want := range []string{"component=river", "river warn at v0", "component=conga", "conga info at v0"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("output lacks %q:\n%s", want, out)
|
|
}
|
|
}
|
|
for _, line := range strings.Split(strings.TrimSpace(out), "\n") {
|
|
if strings.Contains(line, "river warn at v0") && !strings.Contains(line, "component=river") {
|
|
t.Errorf("River line without component=river: %q", line)
|
|
}
|
|
if strings.Contains(line, "conga info at v0") && !strings.Contains(line, "component=conga") {
|
|
t.Errorf("conga line without component=conga: %q", line)
|
|
}
|
|
}
|
|
|
|
buf.Reset()
|
|
diary.SetVerbosity(1, false)
|
|
riverLogger(nil).Info("river info at v1")
|
|
if !strings.Contains(buf.String(), "river info at v1") {
|
|
t.Errorf("River info missing at verbosity 1:\n%s", buf.String())
|
|
}
|
|
}
|
|
|
|
// TestRiverLoggerPublishedOverride: a *slog.Logger the application
|
|
// published wins for River and for conga's own lines; without one, an app
|
|
// still gets the diary loggers.
|
|
func TestRiverLoggerPublishedOverride(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
t.Cleanup(diary.Reset)
|
|
diary.Bootstrap(&buf, diary.Options{Color: bonfire.ColorNever})
|
|
|
|
bare := backpack.New(&compass.Config{})
|
|
riverLogger(bare).Warn("bare app river")
|
|
loggerFromApp(bare).Warn("bare app conga")
|
|
if !strings.Contains(buf.String(), "bare app river") || !strings.Contains(buf.String(), "bare app conga") {
|
|
t.Fatalf("an app without a published logger does not use diary:\n%s", buf.String())
|
|
}
|
|
|
|
var capture bytes.Buffer
|
|
app := backpack.New(&compass.Config{})
|
|
published := slog.New(slog.NewTextHandler(&capture, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
|
if err := app.Publish(published); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
buf.Reset()
|
|
if riverLogger(app) != published || loggerFromApp(app) != published {
|
|
t.Fatal("the published logger is not the one handed out")
|
|
}
|
|
riverLogger(app).Info("published river info")
|
|
loggerFromApp(app).Info("published conga info")
|
|
if !strings.Contains(capture.String(), "published river info") || !strings.Contains(capture.String(), "published conga info") {
|
|
t.Errorf("published logger lacks the records:\n%s", capture.String())
|
|
}
|
|
if buf.Len() != 0 {
|
|
t.Errorf("records reached diary although the app published a logger:\n%s", buf.String())
|
|
}
|
|
}
|