From 15077fff3838b81a1a198afa4ed01ed18bbf62e6 Mon Sep 17 00:00:00 2001 From: Christian Carlsson Date: Wed, 18 Dec 2024 13:02:59 +0000 Subject: [PATCH] chore: logger: seperate error log output (#249) --- server/cmd/main.go | 4 ++-- server/pkg/logger/module.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 server/pkg/logger/module.go diff --git a/server/cmd/main.go b/server/cmd/main.go index bc2e92af..5e27462b 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -6,7 +6,6 @@ import ( "github.com/bufbuild/protovalidate-go" "github.com/joho/godotenv" "go.uber.org/fx" - "go.uber.org/zap" "github.com/crlssn/getstronger/server/bus" "github.com/crlssn/getstronger/server/pkg/config" @@ -14,6 +13,7 @@ import ( "github.com/crlssn/getstronger/server/pkg/db" "github.com/crlssn/getstronger/server/pkg/email" "github.com/crlssn/getstronger/server/pkg/jwt" + "github.com/crlssn/getstronger/server/pkg/logger" "github.com/crlssn/getstronger/server/pkg/repo" "github.com/crlssn/getstronger/server/pkg/stream" "github.com/crlssn/getstronger/server/pkg/trace" @@ -33,9 +33,9 @@ func options() []fx.Option { db.Module(), bus.Module(), jwt.Module(), + logger.Module(), server.Module(), fx.Provide( - zap.NewDevelopment, repo.New, email.New, trace.New, diff --git a/server/pkg/logger/module.go b/server/pkg/logger/module.go new file mode 100644 index 00000000..fe2db4d4 --- /dev/null +++ b/server/pkg/logger/module.go @@ -0,0 +1,30 @@ +package logger + +import ( + "go.uber.org/fx" + "go.uber.org/zap" +) + +func Module() fx.Option { + return fx.Module("logger", fx.Options( + fx.Provide( + func() zap.Config { + return zap.Config{ + Level: zap.NewAtomicLevelAt(zap.InfoLevel), + Development: false, + Sampling: &zap.SamplingConfig{ + Initial: 100, //nolint:mnd + Thereafter: 100, //nolint:mnd + }, + Encoding: "json", + EncoderConfig: zap.NewProductionEncoderConfig(), + OutputPaths: []string{"stdout"}, + ErrorOutputPaths: []string{"stderr"}, + } + }, + func(config zap.Config) (*zap.Logger, error) { + return config.Build() + }, + ), + )) +}