102 lines
1.9 KiB
Go
102 lines
1.9 KiB
Go
package logger
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"cdr.dev/slog/sloggers/slogjson"
|
|
|
|
"cdr.dev/slog/sloggers/sloghuman"
|
|
|
|
"cdr.dev/slog"
|
|
)
|
|
|
|
type Format string
|
|
|
|
const (
|
|
FormatHuman Format = "human"
|
|
FormatJSON Format = "json"
|
|
)
|
|
|
|
var defaultLogger slog.Logger // nolint: gochecknoglobals
|
|
|
|
type Field = slog.Field
|
|
type Map = slog.Map
|
|
type Level = slog.Level
|
|
|
|
const (
|
|
LevelCritical Level = slog.LevelCritical
|
|
LevelError Level = slog.LevelError
|
|
LevelWarn Level = slog.LevelWarn
|
|
LevelInfo Level = slog.LevelInfo
|
|
LevelDebug Level = slog.LevelDebug
|
|
)
|
|
|
|
func init() { // nolint: gochecknoinits
|
|
defaultLogger = Make(FormatHuman, os.Stdout)
|
|
}
|
|
|
|
func Make(f Format, w io.Writer) slog.Logger {
|
|
var logger slog.Logger
|
|
|
|
switch f {
|
|
case FormatHuman:
|
|
logger = sloghuman.Make(w)
|
|
case FormatJSON:
|
|
logger = slogjson.Make(w)
|
|
default:
|
|
panic(errors.Errorf("unknown logger format '%s'", f))
|
|
}
|
|
|
|
return logger
|
|
}
|
|
|
|
func Debug(ctx context.Context, msg string, fields ...Field) {
|
|
slog.Helper()
|
|
defaultLogger.Debug(ctx, msg, fields...)
|
|
}
|
|
|
|
func Info(ctx context.Context, msg string, fields ...Field) {
|
|
slog.Helper()
|
|
defaultLogger.Info(ctx, msg, fields...)
|
|
}
|
|
|
|
func Warn(ctx context.Context, msg string, fields ...Field) {
|
|
slog.Helper()
|
|
defaultLogger.Warn(ctx, msg, fields...)
|
|
}
|
|
|
|
func Error(ctx context.Context, msg string, fields ...Field) {
|
|
slog.Helper()
|
|
defaultLogger.Error(ctx, msg, fields...)
|
|
}
|
|
|
|
func Critical(ctx context.Context, msg string, fields ...Field) {
|
|
slog.Helper()
|
|
defaultLogger.Critical(ctx, msg, fields...)
|
|
}
|
|
|
|
func Fatal(ctx context.Context, msg string, fields ...Field) {
|
|
slog.Helper()
|
|
defaultLogger.Fatal(ctx, msg, fields...)
|
|
}
|
|
|
|
func F(name string, value interface{}) Field {
|
|
return slog.F(name, value)
|
|
}
|
|
|
|
func M(fields ...Field) Map {
|
|
return slog.M(fields...)
|
|
}
|
|
|
|
func E(err error) Field {
|
|
return slog.Error(err)
|
|
}
|
|
|
|
func SetLevel(level Level) {
|
|
defaultLogger = defaultLogger.Leveled(level)
|
|
}
|