tstor/pkg/rlog/rlog.go

141 lines
3.1 KiB
Go
Raw Normal View History

2024-03-19 21:30:37 +00:00
package rlog
import (
2024-04-17 08:36:14 +00:00
"context"
2024-03-19 21:30:37 +00:00
"log/slog"
"os"
2024-04-17 08:36:14 +00:00
"runtime"
"strings"
"time"
2024-03-19 21:30:37 +00:00
"github.com/rs/zerolog"
slogmulti "github.com/samber/slog-multi"
slogzerolog "github.com/samber/slog-zerolog"
)
2024-04-17 08:36:14 +00:00
var (
zl = zerolog.New(&zerolog.ConsoleWriter{Out: os.Stderr})
handlers = []slog.Handler{
slogzerolog.Option{Logger: &zl}.NewZerologHandler(),
}
handler = slogmulti.Fanout(handlers...)
defaultLogger = slog.New(handler)
)
2024-03-19 21:30:37 +00:00
func init() {
slog.SetDefault(defaultLogger)
}
func AddHandler(nh slog.Handler) {
handlers = append(handlers, nh)
2024-04-17 08:36:14 +00:00
handler = slogmulti.Fanout(handlers...)
defaultLogger = slog.New(handler)
2024-03-19 21:30:37 +00:00
slog.SetDefault(defaultLogger)
}
2024-04-17 08:36:14 +00:00
type Logger struct {
handler slog.Handler
component []string
}
const functionKey = "function"
func (l *Logger) log(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
var pcs [1]uintptr
runtime.Callers(3, pcs[:])
pc := pcs[0]
f := runtime.FuncForPC(pc)
attrs = append(attrs, slog.String(functionKey, f.Name()))
r := slog.NewRecord(time.Now(), level, msg, pc)
r.AddAttrs(attrs...)
if ctx == nil {
ctx = context.Background()
}
_ = l.handler.Handle(ctx, r)
}
func (l *Logger) Debug(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelDebug, msg, attrs...)
}
func (l *Logger) Info(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelInfo, msg, attrs...)
}
func (l *Logger) Warn(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelWarn, msg, attrs...)
2024-03-19 21:30:37 +00:00
}
2024-04-17 08:36:14 +00:00
func (l *Logger) Error(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelError, msg, attrs...)
2024-03-19 21:30:37 +00:00
}
2024-04-17 08:36:14 +00:00
const componentKey = "component"
const componentSep = "."
func (log *Logger) WithComponent(name string) *Logger {
c := append(log.component, name)
return &Logger{
handler: log.handler.WithAttrs([]slog.Attr{
slog.String(componentKey, strings.Join(c, componentSep)),
}),
component: c,
}
2024-03-19 21:30:37 +00:00
}
2024-04-17 08:36:14 +00:00
func (l *Logger) With(attrs ...slog.Attr) *Logger {
return &Logger{
handler: l.handler.WithAttrs(attrs),
component: l.component,
}
2024-03-19 21:30:37 +00:00
}
2024-06-14 22:14:44 +00:00
// returns a new slog logger with the same attribures as the original logger
// TODO currently not logging function name
func (l *Logger) Slog() *slog.Logger {
return slog.New(l.handler)
}
2024-04-17 08:36:14 +00:00
const endpointKey = "endpoint"
func (l *Logger) WithEndpoint(name string) *Logger {
return &Logger{
handler: l.handler.WithAttrs([]slog.Attr{
slog.String(endpointKey, name),
}),
component: l.component,
}
2024-03-19 21:30:37 +00:00
}
2024-04-17 08:36:14 +00:00
const errKey = "error"
func Error(err error) slog.Attr {
return slog.Attr{Key: errKey, Value: errValue(err)}
}
// errValue returns a slog.GroupValue with keys "msg" and "trace". If the error
2024-03-19 21:30:37 +00:00
// does not implement interface { StackTrace() errors.StackTrace }, the "trace"
// key is omitted.
2024-04-17 08:36:14 +00:00
func errValue(err error) slog.Value {
2024-03-19 21:30:37 +00:00
if err == nil {
return slog.AnyValue(nil)
}
var groupValues []slog.Attr
2024-04-17 08:36:14 +00:00
groupValues = append(groupValues,
slog.String("msg", err.Error()),
slog.Any("value", err),
)
2024-03-19 21:30:37 +00:00
return slog.GroupValue(groupValues...)
}
2024-04-17 08:36:14 +00:00
2024-06-14 22:14:44 +00:00
func Component(name ...string) *Logger {
2024-04-17 08:36:14 +00:00
return &Logger{
handler: handler,
2024-06-14 22:14:44 +00:00
component: name,
2024-04-17 08:36:14 +00:00
}
}