mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
38 lines
949 B
Go
38 lines
949 B
Go
// Package logging configures structured logging for edge applications.
|
|
package logging
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Options holds configuration options for the structured logger.
|
|
type Options struct {
|
|
AddSource bool
|
|
}
|
|
|
|
// Setup initialises the default slog handler using the given options and the LOG_LEVEL environment variable.
|
|
func Setup(opts Options) {
|
|
handlerOpts := &slog.HandlerOptions{
|
|
AddSource: opts.AddSource,
|
|
Level: ParseLevel(os.Getenv("LOG_LEVEL")),
|
|
}
|
|
handler := slog.NewTextHandler(os.Stdout, handlerOpts)
|
|
slog.SetDefault(slog.New(handler))
|
|
}
|
|
|
|
// ParseLevel converts a log-level string (e.g. "debug", "warn") to the corresponding slog.Level.
|
|
func ParseLevel(value string) slog.Level {
|
|
switch strings.ToLower(strings.TrimSpace(value)) {
|
|
case "debug":
|
|
return slog.LevelDebug
|
|
case "warn", "warning":
|
|
return slog.LevelWarn
|
|
case "error":
|
|
return slog.LevelError
|
|
default:
|
|
return slog.LevelInfo
|
|
}
|
|
}
|