Files
yadu/example/example.go

67 lines
1.2 KiB
Go
Raw Normal View History

2024-01-18 15:03:09 +01:00
package main
import (
"log/slog"
"os"
2024-01-18 18:48:32 +01:00
2025-12-06 20:56:16 +01:00
"github.com/tlinden/yadu/v2"
2024-01-18 15:03:09 +01:00
)
type body string
type Ammo struct {
Forweapon string
Impact int
Cost int
Range int
}
2024-01-22 13:44:19 +01:00
func (a *Ammo) LogValue() slog.Value {
return slog.GroupValue(
slog.String("Forweapon", a.Forweapon),
)
}
2024-01-18 15:03:09 +01:00
type Enemy struct {
Alive bool
Health int
Name string
Body body `yaml:"-"`
Ammo []Ammo
}
2024-01-22 13:44:19 +01:00
func (e *Enemy) LogValue() slog.Value {
return slog.GroupValue(
slog.String("name", e.Name),
)
}
2024-01-18 15:03:09 +01:00
func removeTime(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}
func main() {
2024-01-18 18:48:32 +01:00
opts := &yadu.Options{
2024-01-18 18:49:33 +01:00
Level: slog.LevelDebug,
ReplaceAttr: removeTime,
AddSource: true,
2024-01-18 15:03:09 +01:00
}
2024-01-18 18:48:32 +01:00
logger := slog.New(yadu.NewHandler(os.Stdout, opts))
2024-01-18 15:03:09 +01:00
slog.SetDefault(logger)
e := &Enemy{Alive: true, Health: 10, Name: "Bodo", Body: "body\nbody\n",
Ammo: []Ammo{{Forweapon: "Railgun", Range: 400, Impact: 100, Cost: 100000}},
}
slog.Info("info", "enemy", e, "spawn", 199)
2024-01-22 13:44:19 +01:00
slog.Info("info", "ammo", &Ammo{Forweapon: "axe", Impact: 1})
2024-01-18 15:03:09 +01:00
slog.Info("connecting", "enemies", 100, "players", 2, "world", "600x800")
slog.Debug("debug text")
slog.Error("error")
}