package internal import ( "io" "log/slog" "strings" ) func parseLogLevel(levelStr string) slog.Level { switch strings.ToUpper(levelStr) { case "DEBUG": return slog.LevelDebug case "INFO": return slog.LevelInfo case "WARN": return slog.LevelWarn case "ERROR": return slog.LevelError default: return slog.LevelInfo // Default level } } // InitLoggging initializes global structured logging for the entire application // // Stderr is used for diagnostics and logging. Stdout is used for program // output. Stderr should be used since it will have greater likely hood of being // seen if a programs output is being redirected. func InitLogging(level string, w io.Writer) *slog.Logger { parsedLogLevel := parseLogLevel(level) loggerHandler := slog.NewJSONHandler(w, &slog.HandlerOptions{Level: parsedLogLevel, AddSource: true}) logger := slog.New(loggerHandler) slog.SetDefault(logger) return logger }