Skip to content

Commit

Permalink
fix: multi-log to sentry and console in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Sep 8, 2024
1 parent 0580492 commit b17096e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions config/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit b17096e

Please sign in to comment.