Jelajahi Sumber

日志写本地文件

marion 5 tahun lalu
induk
melakukan
65b64b2b80
1 mengubah file dengan 11 tambahan dan 8 penghapusan
  1. 11 8
      utils/logger/logger.go

+ 11 - 8
utils/logger/logger.go

@@ -7,6 +7,7 @@ import (
 	"github.com/rifflock/lfshook"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/viper"
+	"gopkg.in/gemnasium/logrus-graylog-hook.v2"
 	"os"
 	"reflect"
 	"regexp"
@@ -63,20 +64,22 @@ func New() *Logger {
 	if nil != lfMap && len(lfMap) > 0 {
 		viper.SetDefault("logger.local.file.rotation.hours", 24)
 		viper.SetDefault("logger.local.file.rotation.count", 7)
+		viper.SetDefault("logger.local.file.rotation.postfix", ".%Y%m%d%H%M")
 		rotationHours := viper.GetInt("logger.local.file.rotation.hours")
 		rotationCount := viper.GetInt("logger.local.file.rotation.count")
+		rotationPostfix := viper.GetString("logger.local.file.rotation.postfix")
 		writerMap := lfshook.WriterMap{}
 		if v, ok := lfMap["panic"]; ok {
 			writerMap[logrus.PanicLevel], _ = rotatelogs.New(
-				v+".%Y%m%d",
-				rotatelogs.WithLinkName(v), // 为最新的日志建立软连接,以方便随着找到当前日志文件
+				v+rotationPostfix,
+				rotatelogs.WithLinkName(v),                                          // 为最新的日志建立软连接,以方便随着找到当前日志文件
 				rotatelogs.WithRotationCount(uint(rotationCount)),                   // 设置文件清理前最多保存的个数,也可通过WithMaxAge设置最长保存时间,二者取其一
 				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour), // 设置日志分割的时间,例如一天一次
 			)
 		}
 		if v, ok := lfMap["fatal"]; ok {
 			writerMap[logrus.FatalLevel], _ = rotatelogs.New(
-				v+".%Y%m%d",
+				v+rotationPostfix,
 				rotatelogs.WithLinkName(v),
 				rotatelogs.WithRotationCount(uint(rotationCount)),
 				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
@@ -84,7 +87,7 @@ func New() *Logger {
 		}
 		if v, ok := lfMap["error"]; ok {
 			writerMap[logrus.ErrorLevel], _ = rotatelogs.New(
-				v+".%Y%m%d",
+				v+rotationPostfix,
 				rotatelogs.WithLinkName(v),
 				rotatelogs.WithRotationCount(uint(rotationCount)),
 				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
@@ -92,7 +95,7 @@ func New() *Logger {
 		}
 		if v, ok := lfMap["warn"]; ok {
 			writerMap[logrus.WarnLevel], _ = rotatelogs.New(
-				v+".%Y%m%d",
+				v+rotationPostfix,
 				rotatelogs.WithLinkName(v),
 				rotatelogs.WithRotationCount(uint(rotationCount)),
 				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
@@ -100,7 +103,7 @@ func New() *Logger {
 		}
 		if v, ok := lfMap["info"]; ok {
 			writerMap[logrus.InfoLevel], _ = rotatelogs.New(
-				v+".%Y%m%d",
+				v+rotationPostfix,
 				rotatelogs.WithLinkName(v),
 				rotatelogs.WithRotationCount(uint(rotationCount)),
 				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
@@ -108,7 +111,7 @@ func New() *Logger {
 		}
 		if v, ok := lfMap["debug"]; ok {
 			writerMap[logrus.DebugLevel], _ = rotatelogs.New(
-				v+".%Y%m%d",
+				v+rotationPostfix,
 				rotatelogs.WithLinkName(v),
 				rotatelogs.WithRotationCount(uint(rotationCount)),
 				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
@@ -116,7 +119,7 @@ func New() *Logger {
 		}
 		if v, ok := lfMap["trace"]; ok {
 			writerMap[logrus.TraceLevel], _ = rotatelogs.New(
-				v+".%Y%m%d",
+				v+rotationPostfix,
 				rotatelogs.WithLinkName(v),
 				rotatelogs.WithRotationCount(uint(rotationCount)),
 				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),