make application with base logic
This commit is contained in:
55
internal/common/config/log.go
Normal file
55
internal/common/config/log.go
Normal file
@ -0,0 +1,55 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user