Files
spend-sparrow/internal/log/default.go
Tim Wundenberg 6c92206b3c
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 5m5s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m36s
fix(observabillity): propagate ctx to every log call and add resource to logging
2025-06-17 12:59:43 +02:00

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),
}
}