51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package log
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"go.opentelemetry.io/contrib/bridges/otelslog"
|
|
)
|
|
|
|
func NewLogPropagator() *slog.Logger {
|
|
return slog.New(&logHandler{
|
|
console: slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{}),
|
|
otel: otelslog.NewHandler("spend-sparrow"),
|
|
})
|
|
}
|
|
|
|
type logHandler struct {
|
|
console slog.Handler
|
|
otel slog.Handler
|
|
}
|
|
|
|
// Enabled implements slog.Handler.
|
|
func (l *logHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
|
return l.console.Enabled(ctx, level)
|
|
}
|
|
|
|
// Handle implements slog.Handler.
|
|
func (l *logHandler) Handle(ctx context.Context, rec slog.Record) error {
|
|
if err := l.console.Handle(ctx, rec); err != nil {
|
|
return err
|
|
}
|
|
return l.otel.Handle(ctx, rec)
|
|
}
|
|
|
|
// WithAttrs implements slog.Handler.
|
|
func (l *logHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return &logHandler{
|
|
console: l.console.WithAttrs(attrs),
|
|
otel: l.otel.WithAttrs(attrs),
|
|
}
|
|
}
|
|
|
|
// WithGroup implements slog.Handler.
|
|
func (l *logHandler) WithGroup(name string) slog.Handler {
|
|
return &logHandler{
|
|
console: l.console.WithGroup(name),
|
|
otel: l.otel.WithGroup(name),
|
|
}
|
|
}
|