pcmt/slogging/log.go
leo 8cc43bba13
All checks were successful
continuous-integration/drone/push Build is passing
go: switch to a custom slog-based logging solution
* make all packages use the logger
* add a couple of convenience methods on the logger that enable
  formatting using fmt syntax
2023-04-19 02:12:27 +02:00

45 lines
742 B
Go

package slogging
import (
"fmt"
"os"
"golang.org/x/exp/slog"
)
type Logger struct {
*slog.Logger
}
// var logger *slog.Logger
var logger *Logger
// func Logger() *slog.Logger {
func GetLogger() *Logger {
return logger
}
func Init(jsonHandler bool) error {
if jsonHandler {
logger = &Logger{slog.New(slog.NewJSONHandler(os.Stderr))}
} else {
logger = &Logger{slog.New(slog.NewTextHandler(os.Stderr))}
}
logger.Info("slog logger initialised")
return nil
}
func (l *Logger) Infof(msg string, args ...any) {
l.Info(fmt.Sprintf(msg, args...))
}
func (l *Logger) Warnf(msg string, args ...any) {
l.Warn(fmt.Sprintf(msg, args...))
}
func (l *Logger) Errorf(msg string, args ...any) {
l.Error(fmt.Sprintf(msg, args...))
}