2023-04-19 02:12:27 +02:00
|
|
|
package slogging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"golang.org/x/exp/slog"
|
|
|
|
)
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
type Slogger struct {
|
2023-04-19 02:12:27 +02:00
|
|
|
*slog.Logger
|
|
|
|
}
|
|
|
|
|
2023-04-28 23:01:35 +02:00
|
|
|
const (
|
|
|
|
LevelInfo int = iota
|
|
|
|
LevelDebug
|
|
|
|
LevelWarn
|
|
|
|
LevelError
|
|
|
|
)
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
var logger *Slogger
|
2023-04-19 02:12:27 +02:00
|
|
|
|
2023-04-28 23:01:35 +02:00
|
|
|
var opts slog.HandlerOptions
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
func Logger() *Slogger {
|
2023-04-19 02:12:27 +02:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
func Init(jsonHandler bool) *Slogger {
|
2023-04-28 23:01:35 +02:00
|
|
|
opts = slog.HandlerOptions{
|
|
|
|
Level: slog.LevelInfo,
|
|
|
|
}
|
|
|
|
|
2023-04-19 02:12:27 +02:00
|
|
|
if jsonHandler {
|
2023-05-11 17:06:20 +02:00
|
|
|
logger = &Slogger{slog.New(opts.NewJSONHandler(os.Stderr))}
|
2023-04-19 02:12:27 +02:00
|
|
|
} else {
|
2023-05-11 17:06:20 +02:00
|
|
|
logger = &Slogger{slog.New(opts.NewTextHandler(os.Stderr))}
|
2023-04-19 02:12:27 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:35:00 +02:00
|
|
|
slog.SetDefault(logger.Logger)
|
|
|
|
|
2023-04-19 02:12:27 +02:00
|
|
|
logger.Info("slog logger initialised")
|
|
|
|
|
2023-04-28 22:54:09 +02:00
|
|
|
return logger
|
2023-04-19 02:12:27 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 23:01:35 +02:00
|
|
|
// SetLevel allows setting log level and returns the pointer to the re-created
|
|
|
|
// logger.
|
2023-05-11 17:06:20 +02:00
|
|
|
func SetLevel(level int) *Slogger {
|
2023-04-28 23:01:35 +02:00
|
|
|
switch level {
|
|
|
|
case LevelInfo:
|
|
|
|
opts.Level = slog.LevelInfo
|
|
|
|
|
|
|
|
case LevelDebug:
|
|
|
|
opts.Level = slog.LevelDebug
|
|
|
|
|
|
|
|
case LevelWarn:
|
|
|
|
opts.Level = slog.LevelWarn
|
|
|
|
|
|
|
|
case LevelError:
|
|
|
|
opts.Level = slog.LevelError
|
2023-05-09 13:23:51 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
logger.Infof("unknown level '%d', resetting to level INFO", level)
|
|
|
|
|
|
|
|
opts.Level = slog.LevelInfo
|
2023-04-28 23:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := logger.Handler().(*slog.JSONHandler); ok {
|
2023-05-11 17:06:20 +02:00
|
|
|
logger = &Slogger{slog.New(opts.NewJSONHandler(os.Stderr))}
|
2023-04-28 23:01:35 +02:00
|
|
|
} else {
|
2023-05-11 17:06:20 +02:00
|
|
|
logger = &Slogger{slog.New(opts.NewTextHandler(os.Stderr))}
|
2023-04-28 23:01:35 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:35:00 +02:00
|
|
|
slog.SetDefault(logger.Logger)
|
|
|
|
|
2023-04-28 23:01:35 +02:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
func (l *Slogger) Debugf(msg string, args ...any) {
|
2023-04-28 22:58:50 +02:00
|
|
|
l.Debug(fmt.Sprintf(msg, args...))
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
func (l *Slogger) Infof(msg string, args ...any) {
|
2023-04-19 02:12:27 +02:00
|
|
|
l.Info(fmt.Sprintf(msg, args...))
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
func (l *Slogger) Warnf(msg string, args ...any) {
|
2023-04-19 02:12:27 +02:00
|
|
|
l.Warn(fmt.Sprintf(msg, args...))
|
|
|
|
}
|
|
|
|
|
2023-05-11 17:06:20 +02:00
|
|
|
func (l *Slogger) Errorf(msg string, args ...any) {
|
2023-04-19 02:12:27 +02:00
|
|
|
l.Error(fmt.Sprintf(msg, args...))
|
|
|
|
}
|