log.go 389 B

12345678910111213141516171819202122
  1. package common
  2. import (
  3. "log/slog"
  4. "os"
  5. )
  6. var logger *slog.Logger
  7. func GetLog(prefix string) *slog.Logger {
  8. initLog(prefix)
  9. return logger
  10. }
  11. func initLog(prefix string) {
  12. textHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
  13. AddSource: true,
  14. Level: slog.LevelDebug,
  15. }).WithAttrs([]slog.Attr{slog.String("module", prefix)})
  16. logger = slog.New(textHandler)
  17. }