fix(observabillity): include otel logs
This commit was merged in pull request #158.
This commit is contained in:
@@ -1,14 +1,50 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
// "go.opentelemetry.io/contrib/bridges/otelslog".
|
||||
"os"
|
||||
|
||||
"go.opentelemetry.io/contrib/bridges/otelslog"
|
||||
)
|
||||
|
||||
var (
|
||||
L = slog.New(slog.Default().Handler())
|
||||
)
|
||||
|
||||
func InitOtelLogger() {
|
||||
// L = otelslog.NewLogger("spend-sparrow")
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user