2020-03-17 13:14:53 +01:00
|
|
|
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
|
2020-03-17 13:20:06 +01:00
|
|
|
type Map = slog.Map
|
2020-03-17 13:26:49 +01:00
|
|
|
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
|
|
|
|
)
|
2020-03-17 13:14:53 +01:00
|
|
|
|
|
|
|
func init() { // nolint: gochecknoinits
|
|
|
|
defaultLogger = Make(FormatHuman, os.Stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Make(f Format, w io.Writer) slog.Logger {
|
2021-03-29 12:06:50 +02:00
|
|
|
var sink slog.Sink
|
2020-03-17 13:14:53 +01:00
|
|
|
|
|
|
|
switch f {
|
|
|
|
case FormatHuman:
|
2021-03-29 12:06:50 +02:00
|
|
|
sink = sloghuman.Sink(w)
|
2020-03-17 13:14:53 +01:00
|
|
|
case FormatJSON:
|
2021-03-29 12:06:50 +02:00
|
|
|
sink = slogjson.Sink(w)
|
2020-03-17 13:14:53 +01:00
|
|
|
default:
|
|
|
|
panic(errors.Errorf("unknown logger format '%s'", f))
|
|
|
|
}
|
|
|
|
|
2021-03-29 12:06:50 +02:00
|
|
|
return slog.Make(sink)
|
2020-03-17 13:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-17 13:20:06 +01:00
|
|
|
func M(fields ...Field) Map {
|
|
|
|
return slog.M(fields...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func E(err error) Field {
|
|
|
|
return slog.Error(err)
|
2020-03-17 13:14:53 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:26:49 +01:00
|
|
|
func SetLevel(level Level) {
|
2020-03-17 13:14:53 +01:00
|
|
|
defaultLogger = defaultLogger.Leveled(level)
|
|
|
|
}
|
2020-03-17 13:37:11 +01:00
|
|
|
|
|
|
|
func SetFormat(format Format) {
|
|
|
|
defaultLogger = Make(format, os.Stdout)
|
|
|
|
}
|
2020-03-17 14:10:25 +01:00
|
|
|
|
|
|
|
func With(ctx context.Context, fields ...Field) context.Context {
|
|
|
|
return slog.With(ctx, fields...)
|
|
|
|
}
|