From e76782451be4d9560a79bf45bb87ce5b2ecbc316 Mon Sep 17 00:00:00 2001 From: engigu Date: Sat, 13 Jan 2024 12:04:40 +0800 Subject: [PATCH] update log format --all replace --- main.go | 6 ++++-- middleware/log.go | 4 ++-- migrate/migrate.go | 10 +++++----- pkg/logging/logger.go | 18 +++++++++--------- service/cron_service/cron_service.go | 10 +++++----- service/cron_service/tasks.go | 7 ++++--- service/env_service/env_service.go | 6 +++--- service/send_message_service/consumer.go | 7 ++++--- service/send_message_service/send_message.go | 10 +++++----- 9 files changed, 41 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index d537d84..88aeee1 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,11 @@ import ( "embed" "fmt" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "message-nest/migrate" "message-nest/models" "message-nest/pkg/logging" + "message-nest/pkg/setting" "message-nest/pkg/table" "message-nest/routers" @@ -51,11 +53,11 @@ func GinServerUp() { MaxHeaderBytes: maxHeaderBytes, } - logging.Logger.Info("start http server listening http://0.0.0.0", endPoint) + logrus.Info("start http server listening http://0.0.0.0", endPoint) err := server.ListenAndServe() if err != nil { - logging.Logger.Error("Server err: ", err) + logrus.Error("Server err: ", err) } } diff --git a/middleware/log.go b/middleware/log.go index 4636f2b..65b868d 100644 --- a/middleware/log.go +++ b/middleware/log.go @@ -3,7 +3,7 @@ package middleware import ( "fmt" "math" - "message-nest/pkg/logging" + "net/http" "net/url" "time" @@ -53,7 +53,7 @@ func LogMiddleware(notLogged ...string) gin.HandlerFunc { return } - entry := logging.Logger.WithFields(logrus.Fields{ + entry := logrus.WithFields(logrus.Fields{ ////"hostname": hostname, //"statusCode": statusCode, //"latency": latency, diff --git a/migrate/migrate.go b/migrate/migrate.go index 1aa91e5..eb8355b 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -4,8 +4,8 @@ import ( "errors" "fmt" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "message-nest/models" - "message-nest/pkg/logging" "message-nest/pkg/setting" ) @@ -17,7 +17,7 @@ func InitAuthTableData() { settingO, err := models.GetSettingByKey(initSection, initAuthKey) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - logging.Logger.Error(fmt.Sprintf("查询账号初始化失败!")) + logrus.Error(fmt.Sprintf("查询账号初始化失败!")) return } if settingO.Value == "1" { @@ -26,15 +26,15 @@ func InitAuthTableData() { } err = models.AddUser(initAccount, initAccountPasswd) if err != nil { - logging.Logger.Error(fmt.Sprintf("添加初始化admin账号失败!")) + logrus.Error(fmt.Sprintf("添加初始化admin账号失败!")) return } else { - logging.Logger.Error(fmt.Sprintf("初始化admin账号成功!您的账号:%s 密码:%s", initAccount, initAccountPasswd)) + logrus.Error(fmt.Sprintf("初始化admin账号成功!您的账号:%s 密码:%s", initAccount, initAccountPasswd)) } err = models.AddOneSetting(models.Settings{Section: initSection, Key: initAuthKey, Value: "1"}) if err != nil { - logging.Logger.Error(fmt.Sprintf("标记admin账号初始化状态失败!")) + logrus.Error(fmt.Sprintf("标记admin账号初始化状态失败!")) return } } diff --git a/pkg/logging/logger.go b/pkg/logging/logger.go index 22eeac9..838208b 100644 --- a/pkg/logging/logger.go +++ b/pkg/logging/logger.go @@ -10,7 +10,7 @@ import ( "strings" ) -var Logger = logrus.New() +//var Logger = logrus.New() func getFuncOutStr(funcStr string) string { parts := strings.Split(funcStr, ".") @@ -41,21 +41,21 @@ func Setup() { PrefixStyle: "green", }) - Logger.Formatter = formatter - Logger.SetReportCaller(true) - Logger.SetOutput(os.Stdout) + logrus.SetFormatter(formatter) + logrus.SetReportCaller(true) + logrus.SetOutput(os.Stdout) level := strings.ToLower(setting.AppSetting.LogLevel) switch level { case "debug": - Logger.SetLevel(logrus.DebugLevel) + logrus.SetLevel(logrus.DebugLevel) case "info": - Logger.SetLevel(logrus.InfoLevel) + logrus.SetLevel(logrus.InfoLevel) case "warn": - Logger.SetLevel(logrus.WarnLevel) + logrus.SetLevel(logrus.WarnLevel) case "error": - Logger.SetLevel(logrus.ErrorLevel) + logrus.SetLevel(logrus.ErrorLevel) default: - Logger.SetLevel(logrus.DebugLevel) + logrus.SetLevel(logrus.DebugLevel) } } diff --git a/service/cron_service/cron_service.go b/service/cron_service/cron_service.go index 5e695d8..b6b8274 100644 --- a/service/cron_service/cron_service.go +++ b/service/cron_service/cron_service.go @@ -3,10 +3,10 @@ package cron_service import ( "fmt" "github.com/robfig/cron/v3" + "github.com/sirupsen/logrus" "github.com/unknwon/com" "message-nest/models" "message-nest/pkg/constant" - "message-nest/pkg/logging" "message-nest/service/send_message_service" ) @@ -47,7 +47,7 @@ func (cs *CronService) InitLogsCronRun() { // 注册任务 setting, err := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanCronKeyName) if err != nil { - logging.Logger.Error(fmt.Sprintf("获取日志的cron失败,原因:%s", err)) + logrus.Error(fmt.Sprintf("获取日志的cron失败,原因:%s", err)) } ClearLogsTaskId = AddTask(ScheduledTask{ Schedule: setting.Value, @@ -57,7 +57,7 @@ func (cs *CronService) InitLogsCronRun() { // 添加任务 err = models.AddSendTaskWithID("日志定时清除", constant.CleanLogsTaskId, "admin") if err != nil { - logging.Logger.Error(fmt.Sprintf("添加日志定时清除任务失败,原因:%s", err)) + logrus.Error(fmt.Sprintf("添加日志定时清除任务失败,原因:%s", err)) } } @@ -68,8 +68,8 @@ func (cs *CronService) UpdateLogsCronRun(cron string) { Schedule: cron, Job: ClearLogs, }) - logging.Logger.Error(fmt.Sprintf("更新日志的cron成功,%s", cron)) - logging.Logger.Error(fmt.Sprintf("所有的定时任务: %s", TaskList)) + logrus.Error(fmt.Sprintf("更新日志的cron成功,%s", cron)) + logrus.Error(fmt.Sprintf("所有的定时任务: %s", TaskList)) } diff --git a/service/cron_service/tasks.go b/service/cron_service/tasks.go index ea726a2..0b58b72 100644 --- a/service/cron_service/tasks.go +++ b/service/cron_service/tasks.go @@ -3,7 +3,8 @@ package cron_service import ( "fmt" "github.com/robfig/cron/v3" - "message-nest/pkg/logging" + "github.com/sirupsen/logrus" + "sync" ) @@ -33,10 +34,10 @@ func AddTask(task ScheduledTask) cron.EntryID { taskId, err := CronInstance.AddFunc(task.Schedule, task.Job) if err != nil { // 处理错误 - logging.Logger.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err)) + logrus.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err)) } else { TaskList[taskId] = &task - logging.Logger.Error(fmt.Sprintf("添加定时任务成功,entryID: %d, cron: %s", taskId, task.Schedule)) + logrus.Error(fmt.Sprintf("添加定时任务成功,entryID: %d, cron: %s", taskId, task.Schedule)) } return taskId } diff --git a/service/env_service/env_service.go b/service/env_service/env_service.go index 3edcad3..455e2d1 100644 --- a/service/env_service/env_service.go +++ b/service/env_service/env_service.go @@ -1,9 +1,9 @@ package env_service import ( + "github.com/sirupsen/logrus" "message-nest/models" "message-nest/pkg/constant" - "message-nest/pkg/logging" ) type EnvService struct { @@ -18,9 +18,9 @@ func (es *EnvService) CommonAdd(section string, key string, value string) { Value: value, }) if err != nil { - logging.Logger.Error("初始化%s:%s失败", section, key) + logrus.Error("初始化%s:%s失败", section, key) } else { - logging.Logger.Infof("初始化%s:%s成功", section, key) + logrus.Infof("初始化%s:%s成功", section, key) } } } diff --git a/service/send_message_service/consumer.go b/service/send_message_service/consumer.go index e862904..a1e8430 100644 --- a/service/send_message_service/consumer.go +++ b/service/send_message_service/consumer.go @@ -1,8 +1,9 @@ package send_message_service import ( + "github.com/sirupsen/logrus" "message-nest/pkg/constant" - "message-nest/pkg/logging" + "sync" ) @@ -13,7 +14,7 @@ func DoSendTask(task SendMessageService, wg *sync.WaitGroup) { defer wg.Done() defer func() { if r := recover(); r != nil { - logging.Logger.Error("DoSendTask: Recovered from panic:", r) + logrus.Error("DoSendTask: Recovered from panic:", r) } }() @@ -31,7 +32,7 @@ func MessageConsumer(wg *sync.WaitGroup) { for { task, ok := <-Buffer if !ok { - logging.Logger.Error("MessageConsumer: Channel closed. Exiting.") + logrus.Error("MessageConsumer: Channel closed. Exiting.") return } diff --git a/service/send_message_service/send_message.go b/service/send_message_service/send_message.go index 43f5ef5..16a8bff 100644 --- a/service/send_message_service/send_message.go +++ b/service/send_message_service/send_message.go @@ -2,8 +2,8 @@ package send_message_service import ( "fmt" + "github.com/sirupsen/logrus" "message-nest/models" - "message-nest/pkg/logging" "message-nest/service/send_task_service" "message-nest/service/send_way_service" "strings" @@ -38,7 +38,7 @@ func (sm *SendMessageService) LogsAndStatusMark(errStr string, status int) { if status == SendFail { sm.Status = SendFail } - logging.Logger.Error(fmt.Sprintf("%s, 状态:%d", errStr, status)) + logrus.Error(fmt.Sprintf("%s, 状态:%d", errStr, status)) } // Send 发送一个消息任务的所有实例 @@ -158,7 +158,7 @@ func (sm *SendMessageService) RecordSendLog() { } err := log.Add() if err != nil { - logging.Logger.Error(fmt.Sprintf("添加日志失败!原因是:%s", err)) + logrus.Error(fmt.Sprintf("添加日志失败!原因是:%s", err)) } } @@ -182,10 +182,10 @@ func (sm *SendMessageService) GetSendMsg(ins models.SendTasksIns) (string, strin if !ok || len(content) == 0 { content, ok := data["text"] if !ok { - logging.Logger.Error("text节点数据为空!") + logrus.Error("text节点数据为空!") return "text", "" } else { - logging.Logger.Error(fmt.Sprintf("没有找到%s对应的消息,使用text消息替代!", ins.ContentType)) + logrus.Error(fmt.Sprintf("没有找到%s对应的消息,使用text消息替代!", ins.ContentType)) return "text", content } } else {