diff --git a/apps/server/go.mod b/apps/server/go.mod index 12e07b2..aacb6b7 100644 --- a/apps/server/go.mod +++ b/apps/server/go.mod @@ -11,6 +11,7 @@ require ( github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 + github.com/h2non/bimg v1.1.9 github.com/jackc/pgx/v5 v5.6.0 github.com/joho/godotenv v1.5.1 github.com/kelseyhightower/envconfig v1.4.0 @@ -37,7 +38,6 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/goccy/go-json v0.10.3 // indirect - github.com/h2non/bimg v1.1.9 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect @@ -56,7 +56,6 @@ require ( golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect - golang.org/x/tools v0.26.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/apps/server/go.sum b/apps/server/go.sum index 672297c..fa7a331 100644 --- a/apps/server/go.sum +++ b/apps/server/go.sum @@ -123,8 +123,6 @@ go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= -golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= -golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -134,8 +132,6 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= -golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= @@ -153,8 +149,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -168,16 +162,12 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= -golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= -golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs= google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= diff --git a/apps/server/internal/app/app.go b/apps/server/internal/app/app.go index 187414f..cd5d185 100644 --- a/apps/server/internal/app/app.go +++ b/apps/server/internal/app/app.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/taskemapp/server/apps/server/internal/app/grpcfx" "github.com/taskemapp/server/apps/server/internal/app/profilefx" - "github.com/taskemapp/server/apps/server/internal/logger" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "net/url" "github.com/go-redis/redis/v8" @@ -29,21 +29,9 @@ const ( ) var App = fx.Options( - //TODO(ripls56): temp, will be fixed in next pr - fx.Provide(fx.Annotate(func() (logger.Logger, error) { - c := zap.NewProductionConfig() - c.OutputPaths = []string{"stdout"} - c.ErrorOutputPaths = []string{"stderr"} - - l, err := logger.New(&c) - if err != nil { - return nil, err - } - return l, err - }, fx.As(new(logger.Logger)))), + fx.Provide(fx.Annotate(setupLogger, fx.As(new(logger.Logger)))), fx.Provide(setupConfig), - fx.Provide(setupLogger), fx.Provide(setupPgPool), fx.Provide(setupRabbitMq), fx.Provide(setupRedisClient), @@ -81,19 +69,26 @@ func setupConfig() (config.Config, error) { return cfg, nil } -func setupLogger(c config.Config) *zap.Logger { - var log *zap.Logger +func setupLogger(c config.Config) (logger.Logger, error) { + var zc zap.Config switch c.AppEnv { case envDev: - log, _ = zap.NewDevelopment() + zc = zap.NewDevelopmentConfig() case envProd: - log, _ = zap.NewProduction() + zc = zap.NewProductionConfig() default: - log, _ = zap.NewDevelopment() + zc = zap.NewDevelopmentConfig() } - return log + zc.OutputPaths = []string{"stdout"} + zc.ErrorOutputPaths = []string{"stderr"} + + l, err := logger.New(&zc) + if err != nil { + return nil, err + } + return l, err } func setupPgPool(c config.Config) (*pgxpool.Pool, error) { diff --git a/apps/server/internal/app/authfx/appfx.go b/apps/server/internal/app/authfx/appfx.go index c26a2a3..3b77ff6 100644 --- a/apps/server/internal/app/authfx/appfx.go +++ b/apps/server/internal/app/authfx/appfx.go @@ -3,6 +3,7 @@ package authfx import ( "github.com/taskemapp/server/apps/server/internal/config" authsrv "github.com/taskemapp/server/apps/server/internal/grpc/auth" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/pkg/notifier" "github.com/taskemapp/server/apps/server/internal/repository/token" "github.com/taskemapp/server/apps/server/internal/repository/user" @@ -11,9 +12,17 @@ import ( "go.uber.org/fx" ) +const module = "auth" + var App = fx.Options( fx.Module( - "auth", + module, + fx.Decorate( + func(l logger.Logger) logger.Logger { + return l.WithScope(module) + }, + ), + fx.Provide( fx.Private, fx.Annotate( diff --git a/apps/server/internal/app/grpcfx/appfx.go b/apps/server/internal/app/grpcfx/appfx.go index 627caef..fd1bf61 100644 --- a/apps/server/internal/app/grpcfx/appfx.go +++ b/apps/server/internal/app/grpcfx/appfx.go @@ -10,7 +10,7 @@ import ( "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector" "github.com/taskemapp/server/apps/server/internal/config" "github.com/taskemapp/server/apps/server/internal/grpc/interceptor" - "github.com/taskemapp/server/apps/server/internal/logger" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/repository/token" v1 "github.com/taskemapp/server/apps/server/tools/gen/grpc/v1" "go.uber.org/fx" @@ -23,9 +23,16 @@ import ( "strconv" ) +const module = "grpc" + var App = fx.Options( fx.Module( - "grpc", + module, + fx.Decorate( + func(l logger.Logger) logger.Logger { + return l.WithScope(module) + }, + ), fx.Provide( fx.Private, @@ -36,7 +43,7 @@ var App = fx.Options( ), fx.Invoke( - func(lc fx.Lifecycle, log *zap.Logger, c config.Config, server GrpcServer) { + func(lc fx.Lifecycle, log logger.Logger, c config.Config, server GrpcServer) { lc.Append( fx.Hook{ OnStart: func(ctx context.Context) error { diff --git a/apps/server/internal/app/profilefx/appfx.go b/apps/server/internal/app/profilefx/appfx.go index 8e9d175..197ce41 100644 --- a/apps/server/internal/app/profilefx/appfx.go +++ b/apps/server/internal/app/profilefx/appfx.go @@ -2,6 +2,7 @@ package profilefx import ( profilesrv "github.com/taskemapp/server/apps/server/internal/grpc/profile" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/pkg/s3" "github.com/taskemapp/server/apps/server/internal/repository/user" "github.com/taskemapp/server/apps/server/internal/repository/user_file" @@ -9,15 +10,16 @@ import ( "github.com/taskemapp/server/apps/server/internal/service/profile/image" v1 "github.com/taskemapp/server/apps/server/tools/gen/grpc/v1" "go.uber.org/fx" - "go.uber.org/zap" ) +const module = "profile" + var App = fx.Options( fx.Module( - "profile", + module, fx.Decorate( - func(l *zap.Logger) *zap.Logger { - return l.With(zap.String("scope", "profile")) + func(l logger.Logger) logger.Logger { + return l.WithScope(module) }, ), diff --git a/apps/server/internal/app/taskfx/appfx.go b/apps/server/internal/app/taskfx/appfx.go index 3146b33..14b8eca 100644 --- a/apps/server/internal/app/taskfx/appfx.go +++ b/apps/server/internal/app/taskfx/appfx.go @@ -1,13 +1,21 @@ package taskfx import ( + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/repository/task" "go.uber.org/fx" ) +const module = "task" + var App = fx.Options( fx.Module( - "task", + module, + fx.Decorate( + func(l logger.Logger) logger.Logger { + return l.WithScope(module) + }, + ), fx.Provide( fx.Private, fx.Annotate(task.NewPgx, fx.As(new(task.Repository))), diff --git a/apps/server/internal/app/teamfx/appfx.go b/apps/server/internal/app/teamfx/appfx.go index 83f5036..ce43995 100644 --- a/apps/server/internal/app/teamfx/appfx.go +++ b/apps/server/internal/app/teamfx/appfx.go @@ -2,6 +2,7 @@ package teamfx import ( teamserver "github.com/taskemapp/server/apps/server/internal/grpc/team" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/repository/team" "github.com/taskemapp/server/apps/server/internal/repository/team_member" "github.com/taskemapp/server/apps/server/internal/repository/token" @@ -10,9 +11,17 @@ import ( "go.uber.org/fx" ) +const module = "team" + var App = fx.Options( fx.Module( - "team", + module, + fx.Decorate( + func(l logger.Logger) logger.Logger { + return l.WithScope(module) + }, + ), + fx.Provide( fx.Private, fx.Annotate(team.NewPgx, fx.As(new(team.Repository))), diff --git a/apps/server/internal/grpc/auth/server.go b/apps/server/internal/grpc/auth/server.go index afe43af..6094f05 100644 --- a/apps/server/internal/grpc/auth/server.go +++ b/apps/server/internal/grpc/auth/server.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/taskemapp/server/apps/server/internal/config" "github.com/taskemapp/server/apps/server/internal/grpc/interceptor" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/pkg/validation" "github.com/taskemapp/server/apps/server/internal/repository/token" "github.com/taskemapp/server/apps/server/internal/repository/user" @@ -21,7 +22,7 @@ import ( type Opts struct { fx.In Auth auth.Service - Logger *zap.Logger + Logger logger.Logger Config config.Config TokenRepo token.Repository } @@ -29,7 +30,7 @@ type Opts struct { type Server struct { v1.UnimplementedAuthServer auth auth.Service - logger *zap.Logger + logger logger.Logger config config.Config tokenRepo token.Repository } @@ -63,7 +64,7 @@ func (s *Server) Login( }) if err != nil { - s.logger.Sugar().Error(err) + s.logger.Error("", zap.Error(err)) switch { case errors.Is(err, user.ErrNotFound): return nil, status.Error(codes.NotFound, "Not found") @@ -115,7 +116,7 @@ func (s *Server) SignUp( Password: req.Password, }) if err != nil { - s.logger.Sugar().Error(err) + s.logger.Error("", zap.Error(err)) return nil, status.Error(codes.Internal, err.Error()) } diff --git a/apps/server/internal/grpc/interceptor/auth.go b/apps/server/internal/grpc/interceptor/auth.go index 3ab7a47..5f33ac4 100644 --- a/apps/server/internal/grpc/interceptor/auth.go +++ b/apps/server/internal/grpc/interceptor/auth.go @@ -11,10 +11,6 @@ import ( "google.golang.org/grpc/status" ) -type CtxKey struct { - key string -} - // Auth get token from grpc request metadata // // Already throws formated grpc with status.Errorf diff --git a/apps/server/internal/grpc/interceptor/context.go b/apps/server/internal/grpc/interceptor/context.go index 83567fd..33c9f7e 100644 --- a/apps/server/internal/grpc/interceptor/context.go +++ b/apps/server/internal/grpc/interceptor/context.go @@ -10,6 +10,10 @@ import ( "google.golang.org/grpc/status" ) +type ctxKey struct { + key string +} + func provideUserID(ctx context.Context, payload jwt.Claims) (context.Context, error) { uid, err := uuid.Parse(payload["uid"].(string)) if err != nil { @@ -21,12 +25,12 @@ func provideUserID(ctx context.Context, payload jwt.Claims) (context.Context, er logging.Fields{"user_id", uid.String()}, ) - return context.WithValue(ctx, CtxKey{"uid"}, uid), nil + return context.WithValue(ctx, ctxKey{"uid"}, uid), nil } // GetUserID from context, only throws ErrGetUserID if uid not found in context func GetUserID(ctx context.Context) (uuid.UUID, error) { - if uid, ok := ctx.Value(CtxKey{"uid"}).(uuid.UUID); ok { + if uid, ok := ctx.Value(ctxKey{"uid"}).(uuid.UUID); ok { return uid, nil } @@ -44,12 +48,12 @@ func provideReqID(ctx context.Context) (context.Context, error) { logging.Fields{"request_id", rid.String()}, ) - return context.WithValue(ctx, CtxKey{"rid"}, rid), nil + return context.WithValue(ctx, ctxKey{"rid"}, rid), nil } // GetRequestID from context, only throws ErrRequestID if uid not found in context func GetRequestID(ctx context.Context) (uuid.UUID, error) { - if uid, ok := ctx.Value(CtxKey{"uid"}).(uuid.UUID); ok { + if uid, ok := ctx.Value(ctxKey{"uid"}).(uuid.UUID); ok { return uid, nil } diff --git a/apps/server/internal/grpc/interceptor/interceptor.go b/apps/server/internal/grpc/interceptor/interceptor.go index b76a93e..ff2b09c 100644 --- a/apps/server/internal/grpc/interceptor/interceptor.go +++ b/apps/server/internal/grpc/interceptor/interceptor.go @@ -2,22 +2,22 @@ package interceptor import ( "github.com/taskemapp/server/apps/server/internal/config" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/repository/token" "go.uber.org/fx" - "go.uber.org/zap" ) type Opts struct { fx.In Config config.Config TokenRepo token.Repository - Logger *zap.Logger + Logger logger.Logger } type Interceptor struct { c config.Config tokenRepo token.Repository - logger *zap.Logger + logger logger.Logger } func New(opts Opts) *Interceptor { diff --git a/apps/server/internal/grpc/profile/server.go b/apps/server/internal/grpc/profile/server.go index b1c7ff3..07b6ee9 100644 --- a/apps/server/internal/grpc/profile/server.go +++ b/apps/server/internal/grpc/profile/server.go @@ -3,10 +3,10 @@ package profile import ( "context" "github.com/taskemapp/server/apps/server/internal/grpc/interceptor" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/service/profile" v1 "github.com/taskemapp/server/apps/server/tools/gen/grpc/v1" "go.uber.org/fx" - "go.uber.org/zap" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/emptypb" @@ -14,7 +14,7 @@ import ( type Opts struct { fx.In - Logger *zap.Logger + Logger logger.Logger Profile profile.Service } diff --git a/apps/server/internal/grpc/team/server.go b/apps/server/internal/grpc/team/server.go index deb74ad..8b93596 100644 --- a/apps/server/internal/grpc/team/server.go +++ b/apps/server/internal/grpc/team/server.go @@ -7,6 +7,7 @@ import ( "github.com/taskemapp/server/apps/server/internal/config" "github.com/taskemapp/server/apps/server/internal/grpc/interceptor" "github.com/taskemapp/server/apps/server/internal/mapper" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/repository/token" "github.com/taskemapp/server/apps/server/internal/service" "github.com/taskemapp/server/apps/server/internal/service/team" @@ -22,7 +23,7 @@ type Opts struct { fx.In Team team.Service Config config.Config - Logger *zap.Logger + Logger logger.Logger RedisRepo token.Repository } @@ -30,7 +31,7 @@ type Server struct { v1.UnimplementedTeamServer team team.Service config config.Config - logger *zap.Logger + logger logger.Logger redisRepo token.Repository } @@ -91,7 +92,7 @@ func (t *Server) Create(ctx context.Context, request *v1.CreateTeamRequest) (*v1 Description: request.Description, }) if err != nil { - t.logger.Sugar().Error(err) + t.logger.Error("", zap.Error(err)) switch { case errors.As(err, pgconn.PgError{}): return nil, status.Error(codes.Internal, "Internal server error") diff --git a/apps/server/internal/logger/logger.go b/apps/server/internal/pkg/logger/logger.go similarity index 87% rename from apps/server/internal/logger/logger.go rename to apps/server/internal/pkg/logger/logger.go index 760ffd2..9ca56ef 100644 --- a/apps/server/internal/logger/logger.go +++ b/apps/server/internal/pkg/logger/logger.go @@ -16,6 +16,7 @@ type Logger interface { WithScope(scope string) Logger WithMethod(method string) Logger WithOptions(opts ...zap.Option) Logger + WithComponent(component string) Logger } type logger struct { @@ -73,3 +74,10 @@ func (l *logger) WithOptions(opts ...zap.Option) Logger { Logger: l.Logger.WithOptions(opts...), } } + +func (l *logger) WithComponent(component string) Logger { + return &logger{ + Config: l.Config, + Logger: l.Logger.With(zap.String("component", component)), + } +} diff --git a/apps/server/internal/pkg/migrations/migrations.go b/apps/server/internal/pkg/migrations/migrations.go index c903fc7..0719a71 100644 --- a/apps/server/internal/pkg/migrations/migrations.go +++ b/apps/server/internal/pkg/migrations/migrations.go @@ -4,25 +4,26 @@ import ( "database/sql" "github.com/pressly/goose/v3" "github.com/taskemapp/server/apps/server/internal/config" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "go.uber.org/zap" ) -func Invoke(c config.Config, log *zap.Logger) error { +func Invoke(c config.Config, log logger.Logger) error { if err := goose.SetDialect("pgx"); err != nil { - log.Sugar().Error("Failed to set dialect: ", err) + log.Error("Failed to set dialect: ", zap.Error(err)) return err } db, err := sql.Open("pgx", c.PostgresUrl) if err != nil { - log.Sugar().Error("Failed to open db conn: ", err) + log.Error("Failed to open db conn: ", zap.Error(err)) return err } defer db.Close() - log.Sugar().Info("Run migrations") + log.Info("Run migrations") err = goose.Up(db, "migrations") if err != nil { - log.Sugar().Error("Migration failed: ", err) + log.Error("Migration failed: ", zap.Error(err)) return err } diff --git a/apps/server/internal/pkg/s3/s3.go b/apps/server/internal/pkg/s3/s3.go index 7043c3a..72a68d8 100644 --- a/apps/server/internal/pkg/s3/s3.go +++ b/apps/server/internal/pkg/s3/s3.go @@ -3,6 +3,7 @@ package s3 import ( "context" "fmt" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "time" "github.com/go-faster/errors" @@ -24,7 +25,7 @@ func New(cfg Config) (*minio.Client, error) { return c, nil } -func Invoke(lc fx.Lifecycle, log *zap.Logger, cfg Config, c *minio.Client) { +func Invoke(lc fx.Lifecycle, log logger.Logger, cfg Config, c *minio.Client) { lc.Append( fx.Hook{ OnStart: func(ctx context.Context) error { @@ -37,7 +38,7 @@ func Invoke(lc fx.Lifecycle, log *zap.Logger, cfg Config, c *minio.Client) { return fmt.Errorf("cannot list buckets: %w", err) } - log.Sugar().Debug("Bucket exist: ", ok) + log.Debug("Bucket exist: ", zap.Bool("exist", ok)) if !ok { err = c.MakeBucket(ctx, cfg.Bucket, minio.MakeBucketOptions{}) diff --git a/apps/server/internal/repository/task/pgx.go b/apps/server/internal/repository/task/pgx.go index c59e387..74f064a 100644 --- a/apps/server/internal/repository/task/pgx.go +++ b/apps/server/internal/repository/task/pgx.go @@ -7,8 +7,8 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "go.uber.org/fx" - "go.uber.org/zap" "time" ) @@ -17,12 +17,12 @@ const tableName = "tasks" type Opts struct { fx.In Pgx *pgxpool.Pool - Logger *zap.Logger + Logger logger.Logger } type Pgx struct { pgx *pgxpool.Pool - logger *zap.Logger + logger logger.Logger } func NewPgx(opts Opts) (*Pgx, error) { diff --git a/apps/server/internal/repository/team/pgx.go b/apps/server/internal/repository/team/pgx.go index f6051aa..16981d1 100644 --- a/apps/server/internal/repository/team/pgx.go +++ b/apps/server/internal/repository/team/pgx.go @@ -7,8 +7,8 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "go.uber.org/fx" - "go.uber.org/zap" "time" ) @@ -17,12 +17,12 @@ const tableName = "teams" type Opts struct { fx.In Pgx *pgxpool.Pool - Logger *zap.Logger + Logger logger.Logger } type Pgx struct { pgx *pgxpool.Pool - logger *zap.Logger + logger logger.Logger } func NewPgx(opts Opts) (*Pgx, error) { diff --git a/apps/server/internal/repository/team_member/pgx.go b/apps/server/internal/repository/team_member/pgx.go index 9707ba6..1b9b0f5 100644 --- a/apps/server/internal/repository/team_member/pgx.go +++ b/apps/server/internal/repository/team_member/pgx.go @@ -7,6 +7,7 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "go.uber.org/fx" "go.uber.org/zap" "time" @@ -17,12 +18,12 @@ const tableName = "team_members" type Opts struct { fx.In Pgx *pgxpool.Pool - Logger *zap.Logger + Logger logger.Logger } type Pgx struct { pgx *pgxpool.Pool - logger *zap.Logger + logger logger.Logger } func NewPgx(opts Opts) (*Pgx, error) { @@ -63,7 +64,7 @@ func (p *Pgx) FindByID(ctx context.Context, tmID uuid.UUID) (*TeamMember, error) &tm.IsLeaved, ) if err != nil { - p.logger.Sugar().Error(err) + p.logger.Error("", zap.Error(err)) if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } @@ -98,7 +99,7 @@ func (p *Pgx) FindByUserAndTeam(ctx context.Context, userID uuid.UUID, teamID uu &tm.IsLeaved, ) if err != nil { - p.logger.Sugar().Error(err) + p.logger.Error("", zap.Error(err)) if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } @@ -134,7 +135,7 @@ func (p *Pgx) Create(ctx context.Context, opts CreateOpts) (*TeamMember, error) defer func() { if err != nil { - p.logger.Sugar().Error(err) + p.logger.Error("", zap.Error(err)) _ = tx.Rollback(ctx) } else { _ = tx.Commit(ctx) @@ -151,7 +152,7 @@ func (p *Pgx) Create(ctx context.Context, opts CreateOpts) (*TeamMember, error) &tm.IsLeaved, ) if err != nil { - p.logger.Sugar().Error(err) + p.logger.Error("", zap.Error(err)) return nil, err } @@ -178,7 +179,7 @@ func (p *Pgx) Update(ctx context.Context, tmID uuid.UUID, opts UpdateOpts) (*Tea _, err = p.pgx.Exec(ctx, query, args...) if err != nil { - p.logger.Sugar().Error(err) + p.logger.Error("", zap.Error(err)) return nil, err } diff --git a/apps/server/internal/repository/token/redis.go b/apps/server/internal/repository/token/redis.go index b9a5898..b2b3ac9 100644 --- a/apps/server/internal/repository/token/redis.go +++ b/apps/server/internal/repository/token/redis.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/go-faster/errors" "github.com/go-redis/redis/v8" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "go.uber.org/fx" "go.uber.org/zap" ) @@ -12,12 +13,12 @@ import ( type Opts struct { fx.In Client *redis.Client - Logger *zap.Logger + Logger logger.Logger } type Client struct { client *redis.Client - logger *zap.Logger + logger logger.Logger } func NewClient(opts Opts) (*Client, error) { @@ -36,22 +37,22 @@ func (rc *Client) SetToken(ctx context.Context, opts CreateOpts) error { ).Result() if err != nil { - rc.logger.Sugar().Error("Failed to set token: ", err) + rc.logger.Error("Failed to set token: ", zap.Error(err)) return err } - rc.logger.Sugar().Infof("Token set: %s", val) + rc.logger.Info("Token set: %s", zap.String("ok", val)) return nil } func (rc *Client) GetToken(ctx context.Context, key string) (string, error) { val, err := rc.client.Get(ctx, key).Result() if errors.Is(err, redis.Nil) { - rc.logger.Sugar().Warn("Failed to get token: ", err) + rc.logger.Warn("Failed to get token: ", zap.Error(err)) return "", errors.Wrap(ErrNotFound, "Failed to get token") } if err != nil { - rc.logger.Sugar().Error("Failed to get token: ", err) + rc.logger.Error("Failed to get token: ", zap.Error(err)) return "", errors.Wrap(err, "Failed to get token") } diff --git a/apps/server/internal/repository/user/pgx.go b/apps/server/internal/repository/user/pgx.go index 76f8403..bf6dbdf 100644 --- a/apps/server/internal/repository/user/pgx.go +++ b/apps/server/internal/repository/user/pgx.go @@ -4,7 +4,7 @@ import ( "context" "errors" "github.com/jackc/pgx/v5" - "go.uber.org/zap" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "time" "github.com/Masterminds/squirrel" @@ -18,12 +18,12 @@ const tableName = "users" type Opts struct { fx.In Pgx *pgxpool.Pool - Logger *zap.Logger + Logger logger.Logger } type Pgx struct { pgx *pgxpool.Pool - logger *zap.Logger + logger logger.Logger } func NewPgx(opts Opts) *Pgx { diff --git a/apps/server/internal/service/profile/service.go b/apps/server/internal/service/profile/service.go index fa1b53a..027e938 100644 --- a/apps/server/internal/service/profile/service.go +++ b/apps/server/internal/service/profile/service.go @@ -5,6 +5,7 @@ import ( "context" "github.com/go-faster/errors" "github.com/google/uuid" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/repository/user" "github.com/taskemapp/server/apps/server/internal/repository/user_file" "github.com/taskemapp/server/apps/server/internal/service/profile/image" @@ -17,14 +18,14 @@ type Opts struct { fx.In UserFileRepo user_file.Repository UserRepo user.Repository - Logger *zap.Logger + Logger logger.Logger Processing image.Processing } type Profile struct { userFileRepo user_file.Repository userRepo user.Repository - logger *zap.Logger + logger logger.Logger processing image.Processing } diff --git a/apps/server/internal/service/team/service.go b/apps/server/internal/service/team/service.go index e94fb11..b7bf7b1 100644 --- a/apps/server/internal/service/team/service.go +++ b/apps/server/internal/service/team/service.go @@ -3,6 +3,7 @@ package team import ( "context" "github.com/google/uuid" + "github.com/taskemapp/server/apps/server/internal/pkg/logger" "github.com/taskemapp/server/apps/server/internal/repository/team" "github.com/taskemapp/server/apps/server/internal/repository/team_member" "github.com/taskemapp/server/apps/server/internal/repository/token" @@ -15,22 +16,23 @@ type Opts struct { fx.In TeamRepo team.Repository TeamMemberRepo team_member.Repository - Logger *zap.Logger + Logger logger.Logger RedisRepo token.Repository } type Team struct { teamRepo team.Repository teamMemberRepo team_member.Repository - logger *zap.Logger + logger logger.Logger redisRepo token.Repository } func New(opts Opts) *Team { + l := opts.Logger.WithComponent("team.service") return &Team{ teamRepo: opts.TeamRepo, teamMemberRepo: opts.TeamMemberRepo, - logger: opts.Logger, + logger: l, redisRepo: opts.RedisRepo, } } @@ -38,7 +40,9 @@ func New(opts Opts) *Team { func (t *Team) Get(ctx context.Context, id uuid.UUID) (*team.Team, error) { f, err := t.teamRepo.FindByID(ctx, id) if err != nil { - t.logger.Sugar().Error(err) + t.logger. + WithMethod("get"). + Error("", zap.Error(err)) return nil, err } return f, nil @@ -50,7 +54,9 @@ func (t *Team) GetUserTeams(ctx context.Context, userID uuid.UUID, pgOpts servic PerPage: pgOpts.PerPage, }) if err != nil { - t.logger.Sugar().Error(err) + t.logger. + WithMethod("get_user_teams"). + Error("", zap.Error(err)) return nil, err } return fMany, nil @@ -63,7 +69,9 @@ func (t *Team) Create(ctx context.Context, opts CreateOpts) (*team.Team, error) Creator: opts.CreatorID, }) if err != nil { - t.logger.Sugar().Error(err) + t.logger. + WithMethod("create"). + Error("", zap.Error(err)) return nil, err } return c, nil @@ -75,7 +83,9 @@ func (t *Team) Join(ctx context.Context, opts JoinOpts) error { TeamID: opts.TeamID, }) if err != nil { - t.logger.Sugar().Error(err) + t.logger. + WithMethod("join"). + Error("", zap.Error(err)) return err } return err @@ -84,7 +94,9 @@ func (t *Team) Join(ctx context.Context, opts JoinOpts) error { func (t *Team) Leave(ctx context.Context, opts LeaveOpts) error { tm, err := t.teamMemberRepo.FindByUserAndTeam(ctx, opts.UserID, opts.TeamID) if err != nil { - t.logger.Sugar().Error(err) + t.logger. + WithMethod("leave"). + Error("", zap.Error(err)) return err } @@ -97,7 +109,9 @@ func (t *Team) Leave(ctx context.Context, opts LeaveOpts) error { }, ) if err != nil { - t.logger.Sugar().Error(err) + t.logger. + WithMethod("leave"). + Error("", zap.Error(err)) return err }