feat: add slog structured logging

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
tao.chen
2026-07-02 16:34:42 +08:00
co-authored by Claude Fable 5
parent 8f2f3cf271
commit ea754a85c5
16 changed files with 1432 additions and 82 deletions
+49
View File
@@ -94,3 +94,52 @@ func TestLoadRejectsInvalidDuration(t *testing.T) {
t.Fatal("expected invalid duration error")
}
}
func TestDefaultIncludesLogConfig(t *testing.T) {
cfg := Default()
if cfg.Log.Level != "info" {
t.Fatalf("Log.Level = %q, want info", cfg.Log.Level)
}
if cfg.Log.Format != "json" {
t.Fatalf("Log.Format = %q, want json", cfg.Log.Format)
}
}
func TestLoadParsesLogConfigFromYAML(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
data := []byte(`log:
level: "debug"
format: "text"
`)
if err := os.WriteFile(path, data, 0o644); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if cfg.Log.Level != "debug" {
t.Fatalf("Log.Level = %q, want debug", cfg.Log.Level)
}
if cfg.Log.Format != "text" {
t.Fatalf("Log.Format = %q, want text", cfg.Log.Format)
}
}
func TestLoadAppliesLogEnvironmentOverrides(t *testing.T) {
t.Setenv("CODESPACE_LOG_LEVEL", "warn")
t.Setenv("CODESPACE_LOG_FORMAT", "json")
cfg, err := Load(filepath.Join(t.TempDir(), "missing.yaml"))
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if cfg.Log.Level != "warn" {
t.Fatalf("Log.Level = %q, want warn", cfg.Log.Level)
}
if cfg.Log.Format != "json" {
t.Fatalf("Log.Format = %q, want json", cfg.Log.Format)
}
}