Skip to content

Commit

Permalink
add hook with sentry for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
adlandh committed Sep 1, 2023
1 parent 23f15f4 commit 40d7886
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion internal/post-forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"time"

"github.com/adlandh/echo-sentry-middleware"
Expand All @@ -20,8 +21,31 @@ import (
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func NewLogger(cfg *config.Config) (*zap.Logger, error) {
logger, err := zap.NewDevelopment()
if err != nil {
return nil, err
}

if cfg.SentryDSN == "" {
return logger, nil
}

return logger.WithOptions(zap.Hooks(
func(entry zapcore.Entry) error {
if entry.Level == zapcore.ErrorLevel {
defer sentry.Flush(2 * time.Second)
sentry.CaptureMessage(fmt.Sprintf("%s, Line No: %d :: %s", entry.Caller.File, entry.Caller.Line, entry.Message))
}
return nil
},
)), nil

}

func NewSentry(lc fx.Lifecycle, cfg *config.Config) error {
if cfg.SentryDSN == "" {
return nil
Expand Down Expand Up @@ -117,8 +141,8 @@ func CreateService() fx.Option {
return &fxevent.ZapLogger{Logger: log}
}),
fx.Provide(
zap.NewDevelopment,
config.NewConfig,
NewLogger,
fx.Annotate(
driven.NewTelegramMessageSender,
fx.As(new(domain.MessageDestination)),
Expand Down

0 comments on commit 40d7886

Please sign in to comment.