56 lines
825 B
Go
56 lines
825 B
Go
package config
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
)
|
|
|
|
type LogFormat uint8
|
|
|
|
const (
|
|
LogFormatText LogFormat = iota
|
|
LogFormatJSON
|
|
)
|
|
|
|
type LogLevel uint8
|
|
|
|
const (
|
|
LogLevelDebug LogLevel = iota
|
|
LogLevelInfo
|
|
LogLevelWarn
|
|
LogLevelError
|
|
)
|
|
|
|
type LogConfig struct {
|
|
Level LogLevel
|
|
Format LogFormat
|
|
}
|
|
|
|
func NewSLogger(config LogConfig) *slog.Logger {
|
|
var level slog.Level
|
|
switch config.Level {
|
|
case LogLevelDebug:
|
|
level = slog.LevelDebug
|
|
case LogLevelInfo:
|
|
level = slog.LevelInfo
|
|
case LogLevelWarn:
|
|
level = slog.LevelWarn
|
|
case LogLevelError:
|
|
level = slog.LevelError
|
|
}
|
|
|
|
opts := &slog.HandlerOptions{
|
|
Level: level,
|
|
}
|
|
|
|
var h slog.Handler
|
|
switch config.Format {
|
|
case LogFormatJSON:
|
|
h = slog.NewJSONHandler(os.Stdout, opts)
|
|
case LogFormatText:
|
|
h = slog.NewTextHandler(os.Stdout, opts)
|
|
}
|
|
|
|
return slog.New(h)
|
|
}
|