diff --git a/example/example.go b/example/example.go index a4d7d98..ff25a2d 100644 --- a/example/example.go +++ b/example/example.go @@ -16,6 +16,12 @@ type Ammo struct { Range int } +func (a *Ammo) LogValue() slog.Value { + return slog.GroupValue( + slog.String("Forweapon", a.Forweapon), + ) +} + type Enemy struct { Alive bool Health int @@ -24,6 +30,12 @@ type Enemy struct { Ammo []Ammo } +func (e *Enemy) LogValue() slog.Value { + return slog.GroupValue( + slog.String("name", e.Name), + ) +} + func removeTime(_ []string, a slog.Attr) slog.Attr { if a.Key == slog.TimeKey { return slog.Attr{} @@ -47,6 +59,7 @@ func main() { } slog.Info("info", "enemy", e, "spawn", 199) + slog.Info("info", "ammo", &Ammo{Forweapon: "axe", Impact: 1}) slog.Info("connecting", "enemies", 100, "players", 2, "world", "600x800") slog.Debug("debug text") slog.Error("error") diff --git a/handler.go b/handler.go index d2391e5..b5cf812 100644 --- a/handler.go +++ b/handler.go @@ -75,7 +75,11 @@ func (h *Handler) Handle(ctx context.Context, r slog.Record) error { fields := make(map[string]interface{}, r.NumAttrs()) r.Attrs(func(a slog.Attr) bool { - fields[a.Key] = a.Value.Any() + //fields[a.Key] = a.Value.Any() + a.Value = a.Value.Resolve() + wa := make(map[string]interface{}) + h.appendAttr(wa, a) + fields[a.Key] = wa[a.Key] return true }) diff --git a/handler_test.go b/handler_test.go index 578c4d0..e3d4501 100644 --- a/handler_test.go +++ b/handler_test.go @@ -20,6 +20,12 @@ type Ammo struct { Range float32 } +func (a *Ammo) LogValue() slog.Value { + return slog.GroupValue( + slog.String("Forweapon", "Use weapon: "+a.Forweapon), + ) +} + type Enemy struct { Alive bool Health int @@ -50,6 +56,16 @@ var tests = []Tests{ want: "ammo:", negate: false, }, + { + name: "has-ammo-logvaluer", + want: "Use weapon: Axe", + negate: false, + }, + { + name: "has-ammo-logvaluer-does-resolve", + want: "impact: 50", // should NOT appear + negate: true, + }, { name: "has-alive", want: "alive: true", @@ -144,7 +160,10 @@ func GetEnemy() *Enemy { return &Enemy{Alive: true, Health: 10, Name: "Bodo", Body: "body\nbody\n", Ammo: []Ammo{{Forweapon: "Railgun", Range: 400, Impact: 100, Cost: 100000}}, } +} +func GetAmmo() *Ammo { + return &Ammo{Forweapon: "Axe", Range: 50, Impact: 1, Cost: 50} } func removeTime(_ []string, a slog.Attr) slog.Attr { @@ -174,13 +193,13 @@ func Test(t *testing.T) { switch tt.opts.Level { case slog.LevelDebug: - logger.Debug("attack", "enemy", GetEnemy()) + logger.Debug("attack", "enemy", GetEnemy(), "ammo", GetAmmo()) case slog.LevelWarn: - logger.Warn("attack", "enemy", GetEnemy()) + logger.Warn("attack", "enemy", GetEnemy(), "ammo", GetAmmo()) case slog.LevelError: - logger.Error("attack", "enemy", GetEnemy()) + logger.Error("attack", "enemy", GetEnemy(), "ammo", GetAmmo()) default: - logger.Info("attack", "enemy", GetEnemy()) + logger.Info("attack", "enemy", GetEnemy(), "ammo", GetAmmo()) } got := buf.String()