update log format --all replace

This commit is contained in:
engigu
2024-01-13 12:04:40 +08:00
parent c0241df60c
commit e76782451b
9 changed files with 41 additions and 37 deletions
+4 -2
View File
@@ -4,9 +4,11 @@ import (
"embed" "embed"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"message-nest/migrate" "message-nest/migrate"
"message-nest/models" "message-nest/models"
"message-nest/pkg/logging" "message-nest/pkg/logging"
"message-nest/pkg/setting" "message-nest/pkg/setting"
"message-nest/pkg/table" "message-nest/pkg/table"
"message-nest/routers" "message-nest/routers"
@@ -51,11 +53,11 @@ func GinServerUp() {
MaxHeaderBytes: maxHeaderBytes, 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() err := server.ListenAndServe()
if err != nil { if err != nil {
logging.Logger.Error("Server err: ", err) logrus.Error("Server err: ", err)
} }
} }
+2 -2
View File
@@ -3,7 +3,7 @@ package middleware
import ( import (
"fmt" "fmt"
"math" "math"
"message-nest/pkg/logging"
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
@@ -53,7 +53,7 @@ func LogMiddleware(notLogged ...string) gin.HandlerFunc {
return return
} }
entry := logging.Logger.WithFields(logrus.Fields{ entry := logrus.WithFields(logrus.Fields{
////"hostname": hostname, ////"hostname": hostname,
//"statusCode": statusCode, //"statusCode": statusCode,
//"latency": latency, //"latency": latency,
+5 -5
View File
@@ -4,8 +4,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
"message-nest/models" "message-nest/models"
"message-nest/pkg/logging"
"message-nest/pkg/setting" "message-nest/pkg/setting"
) )
@@ -17,7 +17,7 @@ func InitAuthTableData() {
settingO, err := models.GetSettingByKey(initSection, initAuthKey) settingO, err := models.GetSettingByKey(initSection, initAuthKey)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
logging.Logger.Error(fmt.Sprintf("查询账号初始化失败!")) logrus.Error(fmt.Sprintf("查询账号初始化失败!"))
return return
} }
if settingO.Value == "1" { if settingO.Value == "1" {
@@ -26,15 +26,15 @@ func InitAuthTableData() {
} }
err = models.AddUser(initAccount, initAccountPasswd) err = models.AddUser(initAccount, initAccountPasswd)
if err != nil { if err != nil {
logging.Logger.Error(fmt.Sprintf("添加初始化admin账号失败!")) logrus.Error(fmt.Sprintf("添加初始化admin账号失败!"))
return return
} else { } 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"}) err = models.AddOneSetting(models.Settings{Section: initSection, Key: initAuthKey, Value: "1"})
if err != nil { if err != nil {
logging.Logger.Error(fmt.Sprintf("标记admin账号初始化状态失败!")) logrus.Error(fmt.Sprintf("标记admin账号初始化状态失败!"))
return return
} }
} }
+9 -9
View File
@@ -10,7 +10,7 @@ import (
"strings" "strings"
) )
var Logger = logrus.New() //var Logger = logrus.New()
func getFuncOutStr(funcStr string) string { func getFuncOutStr(funcStr string) string {
parts := strings.Split(funcStr, ".") parts := strings.Split(funcStr, ".")
@@ -41,21 +41,21 @@ func Setup() {
PrefixStyle: "green", PrefixStyle: "green",
}) })
Logger.Formatter = formatter logrus.SetFormatter(formatter)
Logger.SetReportCaller(true) logrus.SetReportCaller(true)
Logger.SetOutput(os.Stdout) logrus.SetOutput(os.Stdout)
level := strings.ToLower(setting.AppSetting.LogLevel) level := strings.ToLower(setting.AppSetting.LogLevel)
switch level { switch level {
case "debug": case "debug":
Logger.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
case "info": case "info":
Logger.SetLevel(logrus.InfoLevel) logrus.SetLevel(logrus.InfoLevel)
case "warn": case "warn":
Logger.SetLevel(logrus.WarnLevel) logrus.SetLevel(logrus.WarnLevel)
case "error": case "error":
Logger.SetLevel(logrus.ErrorLevel) logrus.SetLevel(logrus.ErrorLevel)
default: default:
Logger.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
} }
} }
+5 -5
View File
@@ -3,10 +3,10 @@ package cron_service
import ( import (
"fmt" "fmt"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"github.com/unknwon/com" "github.com/unknwon/com"
"message-nest/models" "message-nest/models"
"message-nest/pkg/constant" "message-nest/pkg/constant"
"message-nest/pkg/logging"
"message-nest/service/send_message_service" "message-nest/service/send_message_service"
) )
@@ -47,7 +47,7 @@ func (cs *CronService) InitLogsCronRun() {
// 注册任务 // 注册任务
setting, err := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanCronKeyName) setting, err := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanCronKeyName)
if err != nil { if err != nil {
logging.Logger.Error(fmt.Sprintf("获取日志的cron失败,原因:%s", err)) logrus.Error(fmt.Sprintf("获取日志的cron失败,原因:%s", err))
} }
ClearLogsTaskId = AddTask(ScheduledTask{ ClearLogsTaskId = AddTask(ScheduledTask{
Schedule: setting.Value, Schedule: setting.Value,
@@ -57,7 +57,7 @@ func (cs *CronService) InitLogsCronRun() {
// 添加任务 // 添加任务
err = models.AddSendTaskWithID("日志定时清除", constant.CleanLogsTaskId, "admin") err = models.AddSendTaskWithID("日志定时清除", constant.CleanLogsTaskId, "admin")
if err != nil { 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, Schedule: cron,
Job: ClearLogs, Job: ClearLogs,
}) })
logging.Logger.Error(fmt.Sprintf("更新日志的cron成功,%s", cron)) logrus.Error(fmt.Sprintf("更新日志的cron成功,%s", cron))
logging.Logger.Error(fmt.Sprintf("所有的定时任务: %s", TaskList)) logrus.Error(fmt.Sprintf("所有的定时任务: %s", TaskList))
} }
+4 -3
View File
@@ -3,7 +3,8 @@ package cron_service
import ( import (
"fmt" "fmt"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"message-nest/pkg/logging" "github.com/sirupsen/logrus"
"sync" "sync"
) )
@@ -33,10 +34,10 @@ func AddTask(task ScheduledTask) cron.EntryID {
taskId, err := CronInstance.AddFunc(task.Schedule, task.Job) taskId, err := CronInstance.AddFunc(task.Schedule, task.Job)
if err != nil { if err != nil {
// 处理错误 // 处理错误
logging.Logger.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err)) logrus.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err))
} else { } else {
TaskList[taskId] = &task 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 return taskId
} }
+3 -3
View File
@@ -1,9 +1,9 @@
package env_service package env_service
import ( import (
"github.com/sirupsen/logrus"
"message-nest/models" "message-nest/models"
"message-nest/pkg/constant" "message-nest/pkg/constant"
"message-nest/pkg/logging"
) )
type EnvService struct { type EnvService struct {
@@ -18,9 +18,9 @@ func (es *EnvService) CommonAdd(section string, key string, value string) {
Value: value, Value: value,
}) })
if err != nil { if err != nil {
logging.Logger.Error("初始化%s:%s失败", section, key) logrus.Error("初始化%s:%s失败", section, key)
} else { } else {
logging.Logger.Infof("初始化%s:%s成功", section, key) logrus.Infof("初始化%s:%s成功", section, key)
} }
} }
} }
+4 -3
View File
@@ -1,8 +1,9 @@
package send_message_service package send_message_service
import ( import (
"github.com/sirupsen/logrus"
"message-nest/pkg/constant" "message-nest/pkg/constant"
"message-nest/pkg/logging"
"sync" "sync"
) )
@@ -13,7 +14,7 @@ func DoSendTask(task SendMessageService, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
defer func() { defer func() {
if r := recover(); r != nil { 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 { for {
task, ok := <-Buffer task, ok := <-Buffer
if !ok { if !ok {
logging.Logger.Error("MessageConsumer: Channel closed. Exiting.") logrus.Error("MessageConsumer: Channel closed. Exiting.")
return return
} }
+5 -5
View File
@@ -2,8 +2,8 @@ package send_message_service
import ( import (
"fmt" "fmt"
"github.com/sirupsen/logrus"
"message-nest/models" "message-nest/models"
"message-nest/pkg/logging"
"message-nest/service/send_task_service" "message-nest/service/send_task_service"
"message-nest/service/send_way_service" "message-nest/service/send_way_service"
"strings" "strings"
@@ -38,7 +38,7 @@ func (sm *SendMessageService) LogsAndStatusMark(errStr string, status int) {
if status == SendFail { if status == SendFail {
sm.Status = SendFail sm.Status = SendFail
} }
logging.Logger.Error(fmt.Sprintf("%s, 状态:%d", errStr, status)) logrus.Error(fmt.Sprintf("%s, 状态:%d", errStr, status))
} }
// Send 发送一个消息任务的所有实例 // Send 发送一个消息任务的所有实例
@@ -158,7 +158,7 @@ func (sm *SendMessageService) RecordSendLog() {
} }
err := log.Add() err := log.Add()
if err != nil { 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 { if !ok || len(content) == 0 {
content, ok := data["text"] content, ok := data["text"]
if !ok { if !ok {
logging.Logger.Error("text节点数据为空!") logrus.Error("text节点数据为空!")
return "text", "" return "text", ""
} else { } else {
logging.Logger.Error(fmt.Sprintf("没有找到%s对应的消息,使用text消息替代!", ins.ContentType)) logrus.Error(fmt.Sprintf("没有找到%s对应的消息,使用text消息替代!", ins.ContentType))
return "text", content return "text", content
} }
} else { } else {