From b17096ed3e438166c08aa7457ec9e549b2267200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 8 Sep 2024 21:24:15 +0200 Subject: [PATCH] fix: multi-log to sentry and console in parallel --- config/sentry.go | 20 +++++++++++++++++--- go.mod | 2 ++ go.sum | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/config/sentry.go b/config/sentry.go index b172cb18..59e9d183 100644 --- a/config/sentry.go +++ b/config/sentry.go @@ -2,6 +2,7 @@ package config import ( "github.com/getsentry/sentry-go" + slogmulti "github.com/samber/slog-multi" slogsentry "github.com/samber/slog-sentry/v2" "log/slog" "net/http" @@ -30,10 +31,23 @@ func Log() *SentryLogger { if Get().IsDev() { level = slog.LevelDebug } - handler := slogsentry.Option{Level: level}.NewSentryHandler() - logger := slog.New(handler) - sentryLogger = &SentryLogger{Logger: logger} + filterRequestInfo := slogmulti.NewWithAttrsInlineMiddleware(func(attrs []slog.Attr, next func([]slog.Attr) slog.Handler) slog.Handler { + attrsNew := []slog.Attr{} + for _, attr := range attrs { + if attr.Key != "request" { + attrsNew = append(attrsNew, attr) + } + } + return next(attrsNew) + }) + + sentryLogger = &SentryLogger{Logger: slog.New( + slogmulti.Fanout( + slogmulti.Pipe(filterRequestInfo).Handler(slog.Default().Handler()), + slogsentry.Option{Level: level}.NewSentryHandler(), + ), + )} return sentryLogger } diff --git a/go.mod b/go.mod index 415e66a5..d25eac7e 100644 --- a/go.mod +++ b/go.mod @@ -42,6 +42,8 @@ require ( gorm.io/gorm v1.25.11 ) +require github.com/samber/slog-multi v1.2.1 // indirect + require ( filippo.io/edwards25519 v1.1.0 // indirect github.com/BurntSushi/toml v1.4.0 // indirect diff --git a/go.sum b/go.sum index f85bdb06..17db8cb2 100644 --- a/go.sum +++ b/go.sum @@ -192,6 +192,8 @@ github.com/samber/lo v1.44.0 h1:5il56KxRE+GHsm1IR+sZ/6J42NODigFiqCWpSc2dybA= github.com/samber/lo v1.44.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= github.com/samber/slog-common v0.17.0 h1:HdRnk7QQTa9ByHlLPK3llCBo8ZSX3F/ZyeqVI5dfMtI= github.com/samber/slog-common v0.17.0/go.mod h1:mZSJhinB4aqHziR0SKPqpVZjJ0JO35JfH+dDIWqaCBk= +github.com/samber/slog-multi v1.2.1 h1:MRVc6JxvGiZ+ubyANneZkMREAFAykoW0CACJZagT7so= +github.com/samber/slog-multi v1.2.1/go.mod h1:uLAvHpGqbYgX4FSL0p1ZwoLuveIAJvBECtE07XmYvFo= github.com/samber/slog-sentry/v2 v2.8.0 h1:XDsokN3fW/vT/LyekgyP6AxWct7YvDwbjrMBSxwVW7Y= github.com/samber/slog-sentry/v2 v2.8.0/go.mod h1:OkeRGrUxkcLqGyePoL8x6lkQkP/N4pWuXOjq3wsRm/k= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=