From 5261741755832bd095cf8ead971186e388db0d65 Mon Sep 17 00:00:00 2001 From: alimy Date: Thu, 9 Jun 2022 18:32:35 +0800 Subject: [PATCH] optimize server logger logic --- internal/conf/conf.go | 3 -- internal/conf/db.go | 3 +- internal/conf/logger.go | 13 +++--- internal/dao/dao.go | 11 ++--- internal/dao/oss_alioss.go | 11 ++--- internal/dao/oss_local.go | 3 +- internal/dao/oss_minio.go | 5 ++- internal/dao/oss_s3.go | 3 +- internal/routers/api/attachment.go | 22 ++++----- internal/routers/api/comment.go | 24 +++++----- internal/routers/api/home.go | 5 ++- internal/routers/api/message.go | 12 ++--- internal/routers/api/post.go | 38 ++++++++-------- internal/routers/api/user.go | 71 +++++++++++++++--------------- internal/routers/router.go | 5 ++- internal/service/post.go | 3 +- internal/service/sign.go | 3 +- 17 files changed, 122 insertions(+), 113 deletions(-) diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 49fa1f53..9e54807a 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -4,8 +4,6 @@ import ( "log" "sync" "time" - - "github.com/sirupsen/logrus" ) var ( @@ -25,7 +23,6 @@ var ( S3Setting *S3SettingS LocalOSSSetting *LocalOSSSettingS JWTSetting *JWTSettingS - Logger *logrus.Logger Mutex *sync.Mutex ) diff --git a/internal/conf/db.go b/internal/conf/db.go index 06dec98b..0cc6799c 100644 --- a/internal/conf/db.go +++ b/internal/conf/db.go @@ -5,6 +5,7 @@ import ( "time" "github.com/go-redis/redis/v8" + "github.com/sirupsen/logrus" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" @@ -19,7 +20,7 @@ var ( func newDBEngine() (*gorm.DB, error) { newLogger := logger.New( - Logger, // io writer(日志输出的目标,前缀和日志包含的内容) + logrus.StandardLogger(), // io writer(日志输出的目标,前缀和日志包含的内容) logger.Config{ SlowThreshold: time.Second, // 慢 SQL 阈值 LogLevel: mySQLSetting.LogLevel, // 日志级别 diff --git a/internal/conf/logger.go b/internal/conf/logger.go index fe36a341..e7f183fe 100644 --- a/internal/conf/logger.go +++ b/internal/conf/logger.go @@ -64,23 +64,24 @@ func (h *zincLogHook) Levels() []logrus.Level { } func setupLogger() { - Logger = logrus.New() - Logger.Formatter = &logrus.JSONFormatter{} + logrus.SetFormatter(&logrus.JSONFormatter{}) if CfgIf("LoggerFile") { - Logger.Out = &lumberjack.Logger{ + out := &lumberjack.Logger{ Filename: loggerFileSetting.SavePath + "/" + loggerFileSetting.FileName + loggerFileSetting.FileExt, MaxSize: 600, MaxAge: 10, LocalTime: true, } + logrus.SetOutput(out) } else if CfgIf("LoggerZinc") { - Logger.Out = io.Discard - Logger.AddHook(&zincLogHook{ + hook := &zincLogHook{ host: loggerZincSetting.Host, index: loggerZincSetting.Index, user: loggerZincSetting.User, password: loggerZincSetting.Password, - }) + } + logrus.SetOutput(io.Discard) + logrus.AddHook(hook) } } diff --git a/internal/dao/dao.go b/internal/dao/dao.go index 7165cbdb..e2ead4d7 100644 --- a/internal/dao/dao.go +++ b/internal/dao/dao.go @@ -6,6 +6,7 @@ import ( "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/pkg/zinc" + "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -55,20 +56,20 @@ func NewDataService(engine *gorm.DB, zinc *zinc.ZincClient) core.DataService { func NewObjectStorageService() (oss core.ObjectStorageService) { if conf.CfgIf("AliOSS") { oss = newAliossServent() - conf.Logger.Infoln("use AliOSS as object storage") + logrus.Infoln("use AliOSS as object storage") } else if conf.CfgIf("MinIO") { oss = newMinioServeant() - conf.Logger.Infoln("use MinIO as object storage") + logrus.Infoln("use MinIO as object storage") } else if conf.CfgIf("S3") { oss = newS3Servent() - conf.Logger.Infoln("use S3 as object storage") + logrus.Infoln("use S3 as object storage") } else if conf.CfgIf("LocalOSS") { oss = newLocalossServent() - conf.Logger.Infoln("use LocalOSS as object storage") + logrus.Infoln("use LocalOSS as object storage") } else { // default use AliOSS oss = newAliossServent() - conf.Logger.Infoln("use default AliOSS as object storage") + logrus.Infoln("use default AliOSS as object storage") } return } diff --git a/internal/dao/oss_alioss.go b/internal/dao/oss_alioss.go index 9fd6083a..21ae949d 100644 --- a/internal/dao/oss_alioss.go +++ b/internal/dao/oss_alioss.go @@ -7,17 +7,18 @@ import ( "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/rocboss/paopao-ce/internal/conf" + "github.com/sirupsen/logrus" ) func newAliossServent() *aliossServant { client, err := oss.New(conf.AliOSSSetting.Endpoint, conf.AliOSSSetting.AccessKeyID, conf.AliOSSSetting.AccessKeySecret) if err != nil { - conf.Logger.Fatalf("alioss.New err: %v", err) + logrus.Fatalf("alioss.New err: %v", err) } bucket, err := client.Bucket(conf.AliOSSSetting.Bucket) if err != nil { - conf.Logger.Fatalf("client.Bucket err: %v", err) + logrus.Fatalf("client.Bucket err: %v", err) } return &aliossServant{ @@ -37,19 +38,19 @@ func (s *aliossServant) PutObject(objectKey string, reader io.Reader, objectSize func (s *aliossServant) SignURL(objectKey string, expiredInSec int64) (string, error) { signedURL, err := s.bucket.SignURL(objectKey, oss.HTTPGet, expiredInSec) if err != nil { - conf.Logger.Errorf("client.SignURL err: %v", err) + logrus.Errorf("client.SignURL err: %v", err) return "", err } ur, err := url.Parse(signedURL) if err != nil { - conf.Logger.Errorf("url.Parse err: %v", err) + logrus.Errorf("url.Parse err: %v", err) return "", err } epath, err := url.PathUnescape(ur.Path) if err != nil { - conf.Logger.Errorf("url.PathUnescape err: %v", err) + logrus.Errorf("url.PathUnescape err: %v", err) return "", err } diff --git a/internal/dao/oss_local.go b/internal/dao/oss_local.go index 99b98106..00ed1b45 100644 --- a/internal/dao/oss_local.go +++ b/internal/dao/oss_local.go @@ -10,12 +10,13 @@ import ( "time" "github.com/rocboss/paopao-ce/internal/conf" + "github.com/sirupsen/logrus" ) func newLocalossServent() *localossServant { savePath, err := filepath.Abs(conf.LocalOSSSetting.SavePath) if err != nil { - conf.Logger.Fatalf("get localOSS save path err: %v", err) + logrus.Fatalf("get localOSS save path err: %v", err) } return &localossServant{ diff --git a/internal/dao/oss_minio.go b/internal/dao/oss_minio.go index e1e80744..053a6c10 100644 --- a/internal/dao/oss_minio.go +++ b/internal/dao/oss_minio.go @@ -10,6 +10,7 @@ import ( "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/rocboss/paopao-ce/internal/conf" + "github.com/sirupsen/logrus" ) func newMinioServeant() *minioServant { @@ -19,7 +20,7 @@ func newMinioServeant() *minioServant { Secure: conf.MinIOSetting.Secure, }) if err != nil { - conf.Logger.Fatalf("minio.New err: %v", err) + logrus.Fatalf("minio.New err: %v", err) } return &minioServant{ client: client, @@ -33,7 +34,7 @@ func (s *minioServant) PutObject(objectKey string, reader io.Reader, objectSize if err != nil { return "", err } - conf.Logger.Infoln("Successfully uploaded bytes: ", uploadInfo) + logrus.Infoln("Successfully uploaded bytes: ", uploadInfo) return s.domain + objectKey, nil } func (s *minioServant) SignURL(objectKey string, expiredInSec int64) (string, error) { diff --git a/internal/dao/oss_s3.go b/internal/dao/oss_s3.go index 7059e429..ee8c1284 100644 --- a/internal/dao/oss_s3.go +++ b/internal/dao/oss_s3.go @@ -4,6 +4,7 @@ import ( "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/rocboss/paopao-ce/internal/conf" + "github.com/sirupsen/logrus" ) func newS3Servent() *s3Servant { @@ -13,7 +14,7 @@ func newS3Servent() *s3Servant { Secure: conf.S3Setting.Secure, }) if err != nil { - conf.Logger.Fatalf("s3.New err: %v", err) + logrus.Fatalf("s3.New err: %v", err) } return &s3Servant{ client: client, diff --git a/internal/routers/api/attachment.go b/internal/routers/api/attachment.go index 13db6895..79e03584 100644 --- a/internal/routers/api/attachment.go +++ b/internal/routers/api/attachment.go @@ -6,12 +6,12 @@ import ( "github.com/disintegration/imaging" "github.com/gin-gonic/gin" "github.com/gofrs/uuid" - "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/convert" "github.com/rocboss/paopao-ce/pkg/errcode" + "github.com/sirupsen/logrus" ) var uploadAttachmentTypeMap = map[string]model.AttachmentType{ @@ -78,7 +78,7 @@ func UploadAttachment(c *gin.Context) { uploadType := c.Request.FormValue("type") file, fileHeader, err := c.Request.FormFile("file") if err != nil { - conf.Logger.Errorf("api.UploadAttachment err: %v", err) + logrus.Errorf("api.UploadAttachment err: %v", err) response.ToErrorResponse(errcode.FileUploadFailed) return } @@ -93,7 +93,7 @@ func UploadAttachment(c *gin.Context) { contentType := fileHeader.Header.Get("Content-Type") fileExt, err := GetFileExt(fileHeader.Header.Get("Content-Type")) if err != nil { - conf.Logger.Errorf("GetFileExt err: %v", err) + logrus.Errorf("GetFileExt err: %v", err) response.ToErrorResponse(err.(*errcode.Error)) return } @@ -104,7 +104,7 @@ func UploadAttachment(c *gin.Context) { objectUrl, err := objectStorage.PutObject(ossSavePath, file, fileHeader.Size, contentType) if err != nil { - conf.Logger.Errorf("putObject err: %v", err) + logrus.Errorf("putObject err: %v", err) response.ToErrorResponse(errcode.FileUploadFailed) return } @@ -130,7 +130,7 @@ func UploadAttachment(c *gin.Context) { attachment, err = service.CreateAttachment(attachment) if err != nil { - conf.Logger.Errorf("service.CreateAttachment err: %v", err) + logrus.Errorf("service.CreateAttachment err: %v", err) response.ToErrorResponse(errcode.FileUploadFailed) } @@ -144,7 +144,7 @@ func DownloadAttachmentPrecheck(c *gin.Context) { // 加载content content, err := service.GetPostContentByID(contentID) if err != nil { - conf.Logger.Errorf("service.GetPostContentByID err: %v", err) + logrus.Errorf("service.GetPostContentByID err: %v", err) response.ToErrorResponse(errcode.InvalidDownloadReq) } user, _ := c.Get("USER") @@ -152,7 +152,7 @@ func DownloadAttachmentPrecheck(c *gin.Context) { // 加载post post, err := service.GetPost(content.PostID) if err != nil { - conf.Logger.Errorf("service.GetPost err: %v", err) + logrus.Errorf("service.GetPost err: %v", err) response.ToResponse(gin.H{ "paid": false, }) @@ -186,7 +186,7 @@ func DownloadAttachment(c *gin.Context) { // 加载content content, err := service.GetPostContentByID(contentID) if err != nil { - conf.Logger.Errorf("service.GetPostContentByID err: %v", err) + logrus.Errorf("service.GetPostContentByID err: %v", err) response.ToErrorResponse(errcode.InvalidDownloadReq) } @@ -197,7 +197,7 @@ func DownloadAttachment(c *gin.Context) { // 加载post post, err := service.GetPost(content.PostID) if err != nil { - conf.Logger.Errorf("service.GetPost err: %v", err) + logrus.Errorf("service.GetPost err: %v", err) response.ToResponse(gin.H{ "paid": false, }) @@ -226,7 +226,7 @@ func DownloadAttachment(c *gin.Context) { AttachmentPrice: post.AttachmentPrice, }, user.(*model.User)) if err != nil { - conf.Logger.Errorf("service.BuyPostAttachment err: %v", err) + logrus.Errorf("service.BuyPostAttachment err: %v", err) if err == errcode.InsuffientDownloadMoney { response.ToErrorResponse(errcode.InsuffientDownloadMoney) @@ -242,7 +242,7 @@ func DownloadAttachment(c *gin.Context) { objectKey := objectStorage.ObjectKey(content.Content) signedURL, err := objectStorage.SignURL(objectKey, 60) if err != nil { - conf.Logger.Errorf("client.SignURL err: %v", err) + logrus.Errorf("client.SignURL err: %v", err) response.ToErrorResponse(errcode.DownloadReqError) return } diff --git a/internal/routers/api/comment.go b/internal/routers/api/comment.go index 6223ab73..4b1fca36 100644 --- a/internal/routers/api/comment.go +++ b/internal/routers/api/comment.go @@ -2,12 +2,12 @@ package api import ( "github.com/gin-gonic/gin" - "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/convert" "github.com/rocboss/paopao-ce/pkg/errcode" + "github.com/sirupsen/logrus" ) func GetPostComments(c *gin.Context) { @@ -17,7 +17,7 @@ func GetPostComments(c *gin.Context) { contents, totalRows, err := service.GetPostComments(postID, "id ASC", 0, 0) if err != nil { - conf.Logger.Errorf("service.GetPostComments err: %v\n", err) + logrus.Errorf("service.GetPostComments err: %v\n", err) response.ToErrorResponse(errcode.GetCommentsFailed) return } @@ -30,7 +30,7 @@ func CreatePostComment(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -42,7 +42,7 @@ func CreatePostComment(c *gin.Context) { if err == errcode.MaxCommentCount { response.ToErrorResponse(errcode.MaxCommentCount) } else { - conf.Logger.Errorf("service.CreatePostComment err: %v\n", err) + logrus.Errorf("service.CreatePostComment err: %v\n", err) response.ToErrorResponse(errcode.CreateCommentFailed) } return @@ -56,7 +56,7 @@ func DeletePostComment(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -64,7 +64,7 @@ func DeletePostComment(c *gin.Context) { comment, err := service.GetPostComment(param.ID) if err != nil { - conf.Logger.Errorf("service.GetPostComment err: %v\n", err) + logrus.Errorf("service.GetPostComment err: %v\n", err) response.ToErrorResponse(errcode.GetCommentFailed) return } @@ -77,7 +77,7 @@ func DeletePostComment(c *gin.Context) { // 执行删除 err = service.DeletePostComment(comment) if err != nil { - conf.Logger.Errorf("service.DeletePostComment err: %v\n", err) + logrus.Errorf("service.DeletePostComment err: %v\n", err) response.ToErrorResponse(errcode.DeleteCommentFailed) return } @@ -90,7 +90,7 @@ func CreatePostCommentReply(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -98,7 +98,7 @@ func CreatePostCommentReply(c *gin.Context) { comment, err := service.CreatePostCommentReply(c, param.CommentID, param.Content, user.(*model.User).ID, param.AtUserID) if err != nil { - conf.Logger.Errorf("service.CreatePostCommentReply err: %v\n", err) + logrus.Errorf("service.CreatePostCommentReply err: %v\n", err) response.ToErrorResponse(errcode.CreateReplyFailed) return } @@ -111,7 +111,7 @@ func DeletePostCommentReply(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -120,7 +120,7 @@ func DeletePostCommentReply(c *gin.Context) { reply, err := service.GetPostCommentReply(param.ID) if err != nil { - conf.Logger.Errorf("service.GetPostCommentReply err: %v\n", err) + logrus.Errorf("service.GetPostCommentReply err: %v\n", err) response.ToErrorResponse(errcode.GetReplyFailed) return } @@ -133,7 +133,7 @@ func DeletePostCommentReply(c *gin.Context) { // 执行删除 err = service.DeletePostCommentReply(reply) if err != nil { - conf.Logger.Errorf("service.DeletePostCommentReply err: %v\n", err) + logrus.Errorf("service.DeletePostCommentReply err: %v\n", err) response.ToErrorResponse(errcode.DeleteCommentFailed) return } diff --git a/internal/routers/api/home.go b/internal/routers/api/home.go index 122d0b35..cbac6926 100644 --- a/internal/routers/api/home.go +++ b/internal/routers/api/home.go @@ -18,6 +18,7 @@ import ( "github.com/rocboss/paopao-ce/pkg/convert" "github.com/rocboss/paopao-ce/pkg/errcode" "github.com/rocboss/paopao-ce/pkg/util" + "github.com/sirupsen/logrus" ) //go:embed assets/comic.ttf @@ -76,7 +77,7 @@ func PostCaptcha(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -96,7 +97,7 @@ func PostCaptcha(c *gin.Context) { err := service.SendPhoneCaptcha(c, param.Phone) if err != nil { - conf.Logger.Errorf("app.SendPhoneCaptcha errs: %v", errs) + logrus.Errorf("app.SendPhoneCaptcha errs: %v", errs) response.ToErrorResponse(errcode.GetPhoneCaptchaError) return } diff --git a/internal/routers/api/message.go b/internal/routers/api/message.go index f6733f07..23a06bbf 100644 --- a/internal/routers/api/message.go +++ b/internal/routers/api/message.go @@ -2,11 +2,11 @@ package api import ( "github.com/gin-gonic/gin" - "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/errcode" + "github.com/sirupsen/logrus" ) func GetUnreadMsgCount(c *gin.Context) { @@ -31,7 +31,7 @@ func GetMessages(c *gin.Context) { messages, totalRows, err := service.GetMessages(userID.(int64), (app.GetPage(c)-1)*app.GetPageSize(c), app.GetPageSize(c)) if err != nil { - conf.Logger.Errorf("service.GetMessages err: %v\n", err) + logrus.Errorf("service.GetMessages err: %v\n", err) response.ToErrorResponse(errcode.GetMessagesFailed) return } @@ -44,7 +44,7 @@ func ReadMessage(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -52,7 +52,7 @@ func ReadMessage(c *gin.Context) { userID, _ := c.Get("UID") err := service.ReadMessage(param.ID, userID.(int64)) if err != nil { - conf.Logger.Errorf("service.ReadMessage err: %v\n", err) + logrus.Errorf("service.ReadMessage err: %v\n", err) response.ToErrorResponse(errcode.ReadMessageFailed) return } @@ -65,7 +65,7 @@ func SendUserWhisper(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -87,7 +87,7 @@ func SendUserWhisper(c *gin.Context) { }) if err != nil { - conf.Logger.Errorf("service.CreateWhisper err: %v\n", err) + logrus.Errorf("service.CreateWhisper err: %v\n", err) if err == errcode.TooManyWhisperNum { response.ToErrorResponse(errcode.TooManyWhisperNum) diff --git a/internal/routers/api/post.go b/internal/routers/api/post.go index 06ac1f77..b595a482 100644 --- a/internal/routers/api/post.go +++ b/internal/routers/api/post.go @@ -2,13 +2,13 @@ package api import ( "github.com/gin-gonic/gin" - "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/convert" "github.com/rocboss/paopao-ce/pkg/errcode" + "github.com/sirupsen/logrus" ) func GetPostList(c *gin.Context) { @@ -32,7 +32,7 @@ func GetPostList(c *gin.Context) { Limit: app.GetPageSize(c), }) if err != nil { - conf.Logger.Errorf("service.GetPostList err: %v\n", err) + logrus.Errorf("service.GetPostList err: %v\n", err) response.ToErrorResponse(errcode.GetPostsFailed) return } @@ -45,7 +45,7 @@ func GetPostList(c *gin.Context) { posts, totalRows, err := service.GetPostListFromSearch(q, (app.GetPage(c)-1)*app.GetPageSize(c), app.GetPageSize(c)) if err != nil { - conf.Logger.Errorf("service.GetPostListFromSearch err: %v\n", err) + logrus.Errorf("service.GetPostListFromSearch err: %v\n", err) response.ToErrorResponse(errcode.GetPostsFailed) return } @@ -60,7 +60,7 @@ func GetPost(c *gin.Context) { postFormated, err := service.GetPost(postID) if err != nil { - conf.Logger.Errorf("service.GetPost err: %v\n", err) + logrus.Errorf("service.GetPost err: %v\n", err) response.ToErrorResponse(errcode.GetPostFailed) return } @@ -73,7 +73,7 @@ func CreatePost(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -82,7 +82,7 @@ func CreatePost(c *gin.Context) { post, err := service.CreatePost(c, userID.(int64), param) if err != nil { - conf.Logger.Errorf("service.CreatePost err: %v\n", err) + logrus.Errorf("service.CreatePost err: %v\n", err) response.ToErrorResponse(errcode.CreatePostFailed) return } @@ -95,7 +95,7 @@ func DeletePost(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -105,7 +105,7 @@ func DeletePost(c *gin.Context) { // 获取Post postFormated, err := service.GetPost(param.ID) if err != nil { - conf.Logger.Errorf("service.GetPost err: %v\n", err) + logrus.Errorf("service.GetPost err: %v\n", err) response.ToErrorResponse(errcode.GetPostFailed) return } @@ -117,7 +117,7 @@ func DeletePost(c *gin.Context) { err = service.DeletePost(param.ID) if err != nil { - conf.Logger.Errorf("service.DeletePost err: %v\n", err) + logrus.Errorf("service.DeletePost err: %v\n", err) response.ToErrorResponse(errcode.DeletePostFailed) return } @@ -150,7 +150,7 @@ func PostStar(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -198,7 +198,7 @@ func PostCollection(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -226,7 +226,7 @@ func LockPost(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -236,7 +236,7 @@ func LockPost(c *gin.Context) { // 获取Post postFormated, err := service.GetPost(param.ID) if err != nil { - conf.Logger.Errorf("service.GetPost err: %v\n", err) + logrus.Errorf("service.GetPost err: %v\n", err) response.ToErrorResponse(errcode.GetPostFailed) return } @@ -247,7 +247,7 @@ func LockPost(c *gin.Context) { } err = service.LockPost(param.ID) if err != nil { - conf.Logger.Errorf("service.LockPost err: %v\n", err) + logrus.Errorf("service.LockPost err: %v\n", err) response.ToErrorResponse(errcode.LockPostFailed) return } @@ -262,7 +262,7 @@ func StickPost(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -272,7 +272,7 @@ func StickPost(c *gin.Context) { // 获取Post postFormated, err := service.GetPost(param.ID) if err != nil { - conf.Logger.Errorf("service.GetPost err: %v\n", err) + logrus.Errorf("service.GetPost err: %v\n", err) response.ToErrorResponse(errcode.GetPostFailed) return } @@ -283,7 +283,7 @@ func StickPost(c *gin.Context) { } err = service.StickPost(param.ID) if err != nil { - conf.Logger.Errorf("service.StickPost err: %v\n", err) + logrus.Errorf("service.StickPost err: %v\n", err) response.ToErrorResponse(errcode.LockPostFailed) return } @@ -298,14 +298,14 @@ func GetPostTags(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } tags, err := service.GetPostTags(¶m) if err != nil { - conf.Logger.Errorf("service.GetPostTags err: %v\n", err) + logrus.Errorf("service.GetPostTags err: %v\n", err) response.ToErrorResponse(errcode.GetPostTagsFailed) return diff --git a/internal/routers/api/user.go b/internal/routers/api/user.go index 89454879..5d5db51a 100644 --- a/internal/routers/api/user.go +++ b/internal/routers/api/user.go @@ -12,6 +12,7 @@ import ( "github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/convert" "github.com/rocboss/paopao-ce/pkg/errcode" + "github.com/sirupsen/logrus" "github.com/smartwalle/alipay/v3" ) @@ -21,21 +22,21 @@ func Login(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } user, err := service.DoLogin(c, ¶m) if err != nil { - conf.Logger.Errorf("service.DoLogin err: %v", err) + logrus.Errorf("service.DoLogin err: %v", err) response.ToErrorResponse(err.(*errcode.Error)) return } token, err := app.GenerateToken(user) if err != nil { - conf.Logger.Errorf("app.GenerateToken err: %v", err) + logrus.Errorf("app.GenerateToken err: %v", err) response.ToErrorResponse(errcode.UnauthorizedTokenGenerate) return } @@ -52,7 +53,7 @@ func Register(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -60,7 +61,7 @@ func Register(c *gin.Context) { // 用户名检查 err := service.ValidUsername(param.Username) if err != nil { - conf.Logger.Errorf("service.Register err: %v", err) + logrus.Errorf("service.Register err: %v", err) response.ToErrorResponse(err.(*errcode.Error)) return } @@ -68,7 +69,7 @@ func Register(c *gin.Context) { // 密码检查 err = service.CheckPassword(param.Password) if err != nil { - conf.Logger.Errorf("service.Register err: %v", err) + logrus.Errorf("service.Register err: %v", err) response.ToErrorResponse(err.(*errcode.Error)) return } @@ -79,7 +80,7 @@ func Register(c *gin.Context) { ) if err != nil { - conf.Logger.Errorf("service.Register err: %v", err) + logrus.Errorf("service.Register err: %v", err) response.ToErrorResponse(errcode.UserRegisterFailed) return } @@ -128,7 +129,7 @@ func ChangeUserPassword(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -141,7 +142,7 @@ func ChangeUserPassword(c *gin.Context) { // 密码检查 err := service.CheckPassword(param.Password) if err != nil { - conf.Logger.Errorf("service.Register err: %v", err) + logrus.Errorf("service.Register err: %v", err) response.ToErrorResponse(err.(*errcode.Error)) return } @@ -165,7 +166,7 @@ func ChangeNickname(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -193,7 +194,7 @@ func ChangeAvatar(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -221,7 +222,7 @@ func BindUserPhone(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -238,7 +239,7 @@ func BindUserPhone(c *gin.Context) { } if err := service.CheckPhoneCaptcha(param.Phone, param.Captcha); err != nil { - conf.Logger.Errorf("service.CheckPhoneCaptcha err: %v\n", err) + logrus.Errorf("service.CheckPhoneCaptcha err: %v\n", err) response.ToErrorResponse(err) return } @@ -256,7 +257,7 @@ func GetUserProfile(c *gin.Context) { user, err := service.GetUserByUsername(username) if err != nil { - conf.Logger.Errorf("service.GetUserByUsername err: %v\n", err) + logrus.Errorf("service.GetUserByUsername err: %v\n", err) response.ToErrorResponse(errcode.NoExistUsername) return } @@ -277,7 +278,7 @@ func GetUserPosts(c *gin.Context) { user, err := service.GetUserByUsername(username) if err != nil { - conf.Logger.Errorf("service.GetUserByUsername err: %v\n", err) + logrus.Errorf("service.GetUserByUsername err: %v\n", err) response.ToErrorResponse(errcode.NoExistUsername) return } @@ -293,7 +294,7 @@ func GetUserPosts(c *gin.Context) { Limit: app.GetPageSize(c), }) if err != nil { - conf.Logger.Errorf("service.GetPostList err: %v\n", err) + logrus.Errorf("service.GetPostList err: %v\n", err) response.ToErrorResponse(errcode.GetPostsFailed) return } @@ -309,7 +310,7 @@ func GetUserCollections(c *gin.Context) { posts, totalRows, err := service.GetUserCollections(userID.(int64), (app.GetPage(c)-1)*app.GetPageSize(c), app.GetPageSize(c)) if err != nil { - conf.Logger.Errorf("service.GetUserCollections err: %v\n", err) + logrus.Errorf("service.GetUserCollections err: %v\n", err) response.ToErrorResponse(errcode.GetCollectionsFailed) return } @@ -323,7 +324,7 @@ func GetUserStars(c *gin.Context) { userID, _ := c.Get("UID") posts, totalRows, err := service.GetUserStars(userID.(int64), (app.GetPage(c)-1)*app.GetPageSize(c), app.GetPageSize(c)) if err != nil { - conf.Logger.Errorf("service.GetUserStars err: %v\n", err) + logrus.Errorf("service.GetUserStars err: %v\n", err) response.ToErrorResponse(errcode.GetCollectionsFailed) return } @@ -337,7 +338,7 @@ func GetSuggestUsers(c *gin.Context) { usernames, err := service.GetSuggestUsers(keyword) if err != nil { - conf.Logger.Errorf("service.GetSuggestUsers err: %v\n", err) + logrus.Errorf("service.GetSuggestUsers err: %v\n", err) response.ToErrorResponse(errcode.GetCollectionsFailed) return } @@ -351,7 +352,7 @@ func GetSuggestTags(c *gin.Context) { tags, err := service.GetSuggestTags(keyword) if err != nil { - conf.Logger.Errorf("service.GetSuggestTags err: %v\n", err) + logrus.Errorf("service.GetSuggestTags err: %v\n", err) response.ToErrorResponse(errcode.GetCollectionsFailed) return } @@ -364,7 +365,7 @@ func GetUserRechargeLink(c *gin.Context) { response := app.NewResponse(c) valid, errs := app.BindAndValid(c, ¶m) if !valid { - conf.Logger.Errorf("app.BindAndValid errs: %v", errs) + logrus.Errorf("app.BindAndValid errs: %v", errs) response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) return } @@ -373,7 +374,7 @@ func GetUserRechargeLink(c *gin.Context) { userID, _ := c.Get("UID") recharge, err := service.CreateRecharge(userID.(int64), param.Amount) if err != nil { - conf.Logger.Errorf("service.CreateRecharge err: %v\n", err) + logrus.Errorf("service.CreateRecharge err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return @@ -382,28 +383,28 @@ func GetUserRechargeLink(c *gin.Context) { client, err := alipay.New(conf.AlipaySetting.AppID, conf.AlipaySetting.PrivateKey, true) // 将 key 的验证调整到初始化阶段 if err != nil { - conf.Logger.Errorf("alipay.New err: %v\n", err) + logrus.Errorf("alipay.New err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } err = client.LoadAppPublicCertFromFile("configs/alipayAppCertPublicKey.crt") // 加载应用公钥证书 if err != nil { - conf.Logger.Errorf("client.LoadAppPublicCertFromFile err: %v\n", err) + logrus.Errorf("client.LoadAppPublicCertFromFile err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } err = client.LoadAliPayRootCertFromFile("configs/alipayRootCert.crt") // 加载支付宝根证书 if err != nil { - conf.Logger.Errorf("client.LoadAliPayRootCertFromFile err: %v\n", err) + logrus.Errorf("client.LoadAliPayRootCertFromFile err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } err = client.LoadAliPayPublicCertFromFile("configs/alipayCertPublicKey_RSA2.crt") // 加载支付宝公钥证书 if err != nil { - conf.Logger.Errorf("client.LoadAliPayPublicCertFromFile err: %v\n", err) + logrus.Errorf("client.LoadAliPayPublicCertFromFile err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } @@ -416,7 +417,7 @@ func GetUserRechargeLink(c *gin.Context) { rsp, err := client.TradePreCreate(p) if err != nil { - conf.Logger.Errorf("client.TradePreCreate err: %v\n", err) + logrus.Errorf("client.TradePreCreate err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } @@ -461,35 +462,35 @@ func AlipayNotify(c *gin.Context) { aliClient, err := alipay.New(conf.AlipaySetting.AppID, conf.AlipaySetting.PrivateKey, true) // 将 key 的验证调整到初始化阶段 if err != nil { - conf.Logger.Errorf("alipay.New err: %v\n", err) + logrus.Errorf("alipay.New err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } err = aliClient.LoadAppPublicCertFromFile("configs/alipayAppCertPublicKey.crt") // 加载应用公钥证书 if err != nil { - conf.Logger.Errorf("client.LoadAppPublicCertFromFile err: %v\n", err) + logrus.Errorf("client.LoadAppPublicCertFromFile err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } err = aliClient.LoadAliPayRootCertFromFile("configs/alipayRootCert.crt") // 加载支付宝根证书 if err != nil { - conf.Logger.Errorf("client.LoadAliPayRootCertFromFile err: %v\n", err) + logrus.Errorf("client.LoadAliPayRootCertFromFile err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } err = aliClient.LoadAliPayPublicCertFromFile("configs/alipayCertPublicKey_RSA2.crt") // 加载支付宝公钥证书 if err != nil { - conf.Logger.Errorf("client.LoadAliPayPublicCertFromFile err: %v\n", err) + logrus.Errorf("client.LoadAliPayPublicCertFromFile err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return } _, err = aliClient.GetTradeNotification(c.Request) if err != nil { - conf.Logger.Errorf("aliClient.GetTradeNotification err: %v\n", err) - conf.Logger.Infoln(c.Request.Form) + logrus.Errorf("aliClient.GetTradeNotification err: %v\n", err) + logrus.Infoln(c.Request.Form) response.ToErrorResponse(errcode.RechargeNotifyError) return } @@ -501,7 +502,7 @@ func AlipayNotify(c *gin.Context) { // 交易支付成功 err = service.FinishRecharge(c, convert.StrTo(id).MustInt64(), tradeNo) if err != nil { - conf.Logger.Errorf("service.FinishRecharge err: %v\n", err) + logrus.Errorf("service.FinishRecharge err: %v\n", err) response.ToErrorResponse(errcode.RechargeNotifyError) return } @@ -516,7 +517,7 @@ func GetUserWalletBills(c *gin.Context) { bills, totalRows, err := service.GetUserWalletBills(userID.(int64), (app.GetPage(c)-1)*app.GetPageSize(c), app.GetPageSize(c)) if err != nil { - conf.Logger.Errorf("service.GetUserWalletBills err: %v\n", err) + logrus.Errorf("service.GetUserWalletBills err: %v\n", err) response.ToErrorResponse(errcode.GetCollectionsFailed) return } diff --git a/internal/routers/router.go b/internal/routers/router.go index 877c1ded..bcd33c60 100644 --- a/internal/routers/router.go +++ b/internal/routers/router.go @@ -9,6 +9,7 @@ import ( "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/middleware" "github.com/rocboss/paopao-ce/internal/routers/api" + "github.com/sirupsen/logrus" ) func NewRouter() *gin.Engine { @@ -198,9 +199,9 @@ func routeLocalOSS(e *gin.Engine) { savePath, err := filepath.Abs(conf.LocalOSSSetting.SavePath) if err != nil { - conf.Logger.Fatalf("get localOSS save path err: %v", err) + logrus.Fatalf("get localOSS save path err: %v", err) } e.Static("/oss", savePath) - conf.Logger.Infof("register LocalOSS route in /oss on save path: %s", savePath) + logrus.Infof("register LocalOSS route in /oss on save path: %s", savePath) } diff --git a/internal/service/post.go b/internal/service/post.go index 2aac1051..f692fb88 100644 --- a/internal/service/post.go +++ b/internal/service/post.go @@ -13,6 +13,7 @@ import ( "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/pkg/util" "github.com/rocboss/paopao-ce/pkg/zinc" + "github.com/sirupsen/logrus" ) type TagType string @@ -109,7 +110,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos for _, item := range param.Contents { if err = item.Check(); err != nil { // 属性非法 - conf.Logger.Infof("contents check err: %v", err) + logrus.Infof("contents check err: %v", err) continue } diff --git a/internal/service/sign.go b/internal/service/sign.go index 61f13030..188eaea6 100644 --- a/internal/service/sign.go +++ b/internal/service/sign.go @@ -6,6 +6,7 @@ import ( "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/pkg/util" + "github.com/sirupsen/logrus" ) func GetParamSign(param map[string]interface{}, secretKey string) string { @@ -24,7 +25,7 @@ func GetParamSign(param map[string]interface{}, secretKey string) string { } if conf.ServerSetting.RunMode == "debug" { - conf.Logger.Info(map[string]string{ + logrus.Info(map[string]string{ "signRaw": signRaw, "sysSign": util.EncodeMD5(signRaw + secretKey), })