You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
925 B
Go

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
}