From fac4f6068b6cbf44b49eb2cf3e3e25c44aaa393f Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 13 Oct 2023 18:01:14 +0200 Subject: [PATCH] Finish switching to use slog for logging --- cmd/rp-archiver/main.go | 49 +++++++++++++--------- go.mod | 10 ++--- go.sum | 36 +++++++--------- utils/logrus.go | 92 ----------------------------------------- 4 files changed, 50 insertions(+), 137 deletions(-) delete mode 100644 utils/logrus.go diff --git a/cmd/rp-archiver/main.go b/cmd/rp-archiver/main.go index d79f811..d2ff793 100644 --- a/cmd/rp-archiver/main.go +++ b/cmd/rp-archiver/main.go @@ -1,6 +1,7 @@ package main import ( + "log" "log/slog" "os" "strings" @@ -8,15 +9,15 @@ import ( "time" "github.com/aws/aws-sdk-go/service/s3/s3iface" - "github.com/evalphobia/logrus_sentry" + "github.com/getsentry/sentry-go" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" "github.com/nyaruka/ezconf" "github.com/nyaruka/gocommon/analytics" "github.com/nyaruka/gocommon/dates" "github.com/nyaruka/rp-archiver/archives" - "github.com/nyaruka/rp-archiver/utils" - "github.com/sirupsen/logrus" + slogmulti "github.com/samber/slog-multi" + slogsentry "github.com/samber/slog-sentry" ) var ( @@ -31,36 +32,44 @@ func main() { loader.MustLoad() if config.KeepFiles && !config.UploadToS3 { - logrus.Fatal("cannot delete archives and also not upload to s3") + log.Fatal("cannot delete archives and also not upload to s3") } - level, err := logrus.ParseLevel(config.LogLevel) + var level slog.Level + err := level.UnmarshalText([]byte(config.LogLevel)) if err != nil { - logrus.Fatalf("Invalid log level '%s'", level) + log.Fatalf("invalid log level %s", level) + os.Exit(1) } - logrus.SetLevel(level) - logrus.SetOutput(os.Stdout) - logrus.SetFormatter(&logrus.TextFormatter{}) - logrus.WithField("version", version).WithField("released", date).Info("starting archiver") - - // configure golang std structured logging to route to logrus - slog.SetDefault(slog.New(utils.NewLogrusHandler(logrus.StandardLogger()))) + // configure our logger + logHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level}) + slog.SetDefault(slog.New(logHandler)) logger := slog.With("comp", "main") logger.Info("starting archiver", "version", version, "released", date) // if we have a DSN entry, try to initialize it if config.SentryDSN != "" { - hook, err := logrus_sentry.NewSentryHook(config.SentryDSN, []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}) - hook.Timeout = 0 - hook.StacktraceConfiguration.Enable = true - hook.StacktraceConfiguration.Skip = 4 - hook.StacktraceConfiguration.Context = 5 + err := sentry.Init(sentry.ClientOptions{ + Dsn: config.SentryDSN, + EnableTracing: false, + }) if err != nil { - logrus.Fatalf("invalid sentry DSN: '%s': %s", config.SentryDSN, err) + log.Fatalf("error initiating sentry client, error %s, dsn %s", err, config.SentryDSN) + os.Exit(1) } - logrus.StandardLogger().Hooks.Add(hook) + + defer sentry.Flush(2 * time.Second) + + logger = slog.New( + slogmulti.Fanout( + logHandler, + slogsentry.Option{Level: slog.LevelError}.NewSentryHandler(), + ), + ) + logger = logger.With("release", version) + slog.SetDefault(logger) } // our settings shouldn't contain a timezone, nothing will work right with this not being a constant UTC diff --git a/go.mod b/go.mod index fcc34de..e3002cb 100644 --- a/go.mod +++ b/go.mod @@ -4,21 +4,20 @@ go 1.22 require ( github.com/aws/aws-sdk-go v1.51.20 - github.com/evalphobia/logrus_sentry v0.8.2 + github.com/getsentry/sentry-go v0.22.0 github.com/jmoiron/sqlx v1.3.5 github.com/lib/pq v1.10.9 github.com/nyaruka/ezconf v0.3.0 github.com/nyaruka/gocommon v1.53.2 github.com/pkg/errors v0.9.1 - github.com/sirupsen/logrus v1.9.3 + github.com/samber/slog-multi v1.0.2 + github.com/samber/slog-sentry v1.2.2 github.com/stretchr/testify v1.9.0 ) require ( - github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/structs v1.1.0 // indirect - github.com/getsentry/raven-go v0.2.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/kylelemons/godebug v1.1.0 // indirect github.com/naoina/go-stringutil v0.1.0 // indirect @@ -27,7 +26,8 @@ require ( github.com/nyaruka/null/v2 v2.0.3 // indirect github.com/nyaruka/phonenumbers v1.3.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 // indirect + github.com/samber/lo v1.38.1 // indirect + golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect golang.org/x/sys v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.33.0 // indirect diff --git a/go.sum b/go.sum index 2a84d2f..51f1390 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,18 @@ github.com/aws/aws-sdk-go v1.51.20 h1:ziM90ujYHKKkoTZL+Wg2LwjbQecL+l298GGJeG4ktZs= github.com/aws/aws-sdk-go v1.51.20/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= -github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s= -github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/evalphobia/logrus_sentry v0.8.2 h1:dotxHq+YLZsT1Bb45bB5UQbfCh3gM/nFFetyN46VoDQ= -github.com/evalphobia/logrus_sentry v0.8.2/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= -github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs= -github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= +github.com/getsentry/sentry-go v0.22.0 h1:XNX9zKbv7baSEI65l+H1GEJgSeIC1c7EN5kluWaP6dM= +github.com/getsentry/sentry-go v0.22.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= +github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= +github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -42,27 +40,25 @@ github.com/nyaruka/null/v2 v2.0.3 h1:rdmMRQyVzrOF3Jff/gpU/7BDR9mQX0lcLl4yImsA3kw github.com/nyaruka/null/v2 v2.0.3/go.mod h1:OCVeCkCXwrg5/qE6RU0c1oUVZBy+ZDrT+xYg1XSaIWA= github.com/nyaruka/phonenumbers v1.3.4 h1:bF1Wdh++fxw09s3surhVeBhXEcUKG07pHeP8HQXqjn8= github.com/nyaruka/phonenumbers v1.3.4/go.mod h1:Ut+eFwikULbmCenH6InMKL9csUNLyxHuBLyfkpum11s= +github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= +github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= +github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/samber/slog-multi v1.0.2 h1:6BVH9uHGAsiGkbbtQgAOQJMpKgV8unMrHhhJaw+X1EQ= +github.com/samber/slog-multi v1.0.2/go.mod h1:uLAvHpGqbYgX4FSL0p1ZwoLuveIAJvBECtE07XmYvFo= +github.com/samber/slog-sentry v1.2.2 h1:S0glIVITlGCCfSvIOte2Sh63HMHJpYN3hDr+97hILIk= +github.com/samber/slog-sentry v1.2.2/go.mod h1:bHm8jm1dks0p+xc/lH2i4TIFwnPcMTvZeHgCBj5+uhA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ= golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= -golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 h1:ESSUROHIBHg7USnszlcdmjBEwdMj9VUvU+OPk4yl2mc= -golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= @@ -71,8 +67,8 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/utils/logrus.go b/utils/logrus.go deleted file mode 100644 index 2650bf9..0000000 --- a/utils/logrus.go +++ /dev/null @@ -1,92 +0,0 @@ -// Structured logging handler for logrus so we can rewrite code to use slog package incrementally. Once all logging is -// happening via slog, we just need to hook up Sentry directly to that, and then we can get rid of this file. -package utils - -import ( - "context" - "log/slog" - "slices" - "strings" - - "github.com/sirupsen/logrus" -) - -var levels = map[slog.Level]logrus.Level{ - slog.LevelError: logrus.ErrorLevel, - slog.LevelWarn: logrus.WarnLevel, - slog.LevelInfo: logrus.InfoLevel, - slog.LevelDebug: logrus.DebugLevel, -} - -type LogrusHandler struct { - logger *logrus.Logger - groups []string - attrs []slog.Attr -} - -func NewLogrusHandler(logger *logrus.Logger) *LogrusHandler { - return &LogrusHandler{logger: logger} -} - -func (l *LogrusHandler) clone() *LogrusHandler { - return &LogrusHandler{ - logger: l.logger, - groups: slices.Clip(l.groups), - attrs: slices.Clip(l.attrs), - } -} - -func (l *LogrusHandler) Enabled(ctx context.Context, level slog.Level) bool { - return levels[level] <= l.logger.GetLevel() -} - -func (l *LogrusHandler) Handle(ctx context.Context, r slog.Record) error { - log := logrus.NewEntry(l.logger) - if r.Time.IsZero() { - log = log.WithTime(r.Time) - } - - f := logrus.Fields{} - for _, a := range l.attrs { - if a.Key != "" { - f[a.Key] = a.Value - } - } - log = log.WithFields(f) - - r.Attrs(func(attr slog.Attr) bool { - if attr.Key == "" { - return true - } - log = log.WithField(attr.Key, attr.Value) - return true - }) - log.Logf(levels[r.Level], r.Message) - return nil -} - -func (l *LogrusHandler) groupPrefix() string { - if len(l.groups) > 0 { - return strings.Join(l.groups, ":") + ":" - } - return "" -} - -func (l *LogrusHandler) WithAttrs(attrs []slog.Attr) slog.Handler { - newHandler := l.clone() - for _, a := range attrs { - newHandler.attrs = append(newHandler.attrs, slog.Attr{ - Key: l.groupPrefix() + a.Key, - Value: a.Value, - }) - } - return newHandler -} - -func (l *LogrusHandler) WithGroup(name string) slog.Handler { - newHandler := l.clone() - newHandler.groups = append(newHandler.groups, name) - return newHandler -} - -var _ slog.Handler = &LogrusHandler{}