From 9463ec4cb063f0bde03d0e1b2a650f5887ad29ff Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 17 Aug 2025 15:16:01 +0200 Subject: use slog and add game log handler callback --- go/log/log.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 go/log/log.go (limited to 'go/log') diff --git a/go/log/log.go b/go/log/log.go new file mode 100644 index 00000000..ff090425 --- /dev/null +++ b/go/log/log.go @@ -0,0 +1,61 @@ +package log + +import ( + "context" + "fmt" + "log" + "log/slog" + "os" +) + +const GameLogLevel = 99 + +var handleGameRecord func(any) + +func init() { + // TODO: allow to specify the value and the file + l := slog.New(slog.NewTextHandler( + os.Stdout, + &slog.HandlerOptions{AddSource: true})) + slog.SetDefault(l) +} + +func Game(obj any) { + if handleGameRecord != nil { + handleGameRecord(obj) + } + msg := fmt.Sprintf("%s", obj) + slog.Log(context.Background(), GameLogLevel, msg) +} + +func Debug(msg string, args ...any) { + slog.Debug(msg, args...) +} + +func Info(msg string, args ...any) { + slog.Info(msg, args...) +} + +func Warn(msg string, args ...any) { + slog.Warn(msg, args...) +} + +func Error(msg string, args ...any) { + slog.Error(msg, args...) +} + +func Fatal(args ...any) { + log.Fatal(args...) +} + +func Fatalf(msg string, args ...any) { + log.Fatalf(msg, args...) +} + +func Panic(args ...any) { + log.Panic(args...) +} + +func Panicf(msg string, args ...any) { + log.Panicf(msg, args...) +} -- cgit v1.2.3