marion преди 5 години
родител
ревизия
cdf69a9eac
променени са 1 файла, в които са добавени 84 реда и са изтрити 4 реда
  1. 84 4
      utils/logger/logger.go

+ 84 - 4
utils/logger/logger.go

@@ -3,6 +3,8 @@ package logger
 import (
 	"database/sql/driver"
 	"fmt"
+	"github.com/lestrrat-go/file-rotatelogs"
+	"github.com/rifflock/lfshook"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/viper"
 	"gopkg.in/gemnasium/logrus-graylog-hook.v2"
@@ -42,9 +44,15 @@ func New() *Logger {
 			level = logrus.PanicLevel
 		}
 	}
+	var formatter logrus.Formatter
+	if viper.GetString("logger.formatter") == "json" {
+		formatter = &logrus.JSONFormatter{TimestampFormat: timeFormat}
+	} else {
+		formatter = &logrus.TextFormatter{TimestampFormat: timeFormat}
+	}
 	log := &logrus.Logger{
 		Out:       os.Stderr,
-		Formatter: &logrus.TextFormatter{TimestampFormat: timeFormat},
+		Formatter: formatter,
 		Hooks:     make(logrus.LevelHooks),
 		Level:     level,
 	}
@@ -52,6 +60,78 @@ func New() *Logger {
 		grayHook := graylog.NewGraylogHook(grayAddr, nil)
 		log.AddHook(grayHook)
 	}
+	lfMap := viper.GetStringMapString("logger.local.file.path")
+	if nil != lfMap && len(lfMap) > 0 {
+		viper.SetDefault("logger.local.file.rotation.hours", 24)
+		viper.SetDefault("logger.local.file.rotation.count", 7)
+		rotationHours := viper.GetInt("logger.local.file.rotation.hours")
+		rotationCount := viper.GetInt("logger.local.file.rotation.count")
+		writerMap := lfshook.WriterMap{}
+		if v, ok := lfMap["panic"]; ok {
+			writerMap[logrus.PanicLevel], _ = rotatelogs.New(
+				v+".%Y%m%d",
+				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",
+				rotatelogs.WithLinkName(v),
+				rotatelogs.WithRotationCount(uint(rotationCount)),
+				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
+			)
+		}
+		if v, ok := lfMap["error"]; ok {
+			writerMap[logrus.ErrorLevel], _ = rotatelogs.New(
+				v+".%Y%m%d",
+				rotatelogs.WithLinkName(v),
+				rotatelogs.WithRotationCount(uint(rotationCount)),
+				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
+			)
+		}
+		if v, ok := lfMap["warn"]; ok {
+			writerMap[logrus.WarnLevel], _ = rotatelogs.New(
+				v+".%Y%m%d",
+				rotatelogs.WithLinkName(v),
+				rotatelogs.WithRotationCount(uint(rotationCount)),
+				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
+			)
+		}
+		if v, ok := lfMap["info"]; ok {
+			writerMap[logrus.InfoLevel], _ = rotatelogs.New(
+				v+".%Y%m%d",
+				rotatelogs.WithLinkName(v),
+				rotatelogs.WithRotationCount(uint(rotationCount)),
+				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
+			)
+		}
+		if v, ok := lfMap["debug"]; ok {
+			writerMap[logrus.DebugLevel], _ = rotatelogs.New(
+				v+".%Y%m%d",
+				rotatelogs.WithLinkName(v),
+				rotatelogs.WithRotationCount(uint(rotationCount)),
+				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
+			)
+		}
+		if v, ok := lfMap["trace"]; ok {
+			writerMap[logrus.TraceLevel], _ = rotatelogs.New(
+				v+".%Y%m%d",
+				rotatelogs.WithLinkName(v),
+				rotatelogs.WithRotationCount(uint(rotationCount)),
+				rotatelogs.WithRotationTime(time.Duration(rotationHours)*time.Hour),
+			)
+		}
+		var lfFormatter logrus.Formatter
+		if viper.GetString("logger.local.file.formatter") == "json" {
+			lfFormatter = &logrus.JSONFormatter{TimestampFormat: timeFormat}
+		} else {
+			lfFormatter = &logrus.TextFormatter{TimestampFormat: timeFormat}
+		}
+		lfHook := lfshook.NewHook(writerMap, lfFormatter)
+		log.AddHook(lfHook)
+	}
 	entry := logrus.NewEntry(log)
 	extra := viper.GetStringMap("logger.extra")
 	if nil != extra && len(extra) > 0 {
@@ -81,7 +161,7 @@ func (logger *Logger) WithField(key string, value interface{}) *Logger {
 }
 
 func (logger *Logger) WithFields(fields map[string]interface{}) *Logger {
- 	return &Logger{Entry: *logger.Entry.WithFields(fields)}
+	return &Logger{Entry: *logger.Entry.WithFields(fields)}
 }
 
 func (logger *Logger) WithError(err error) *Logger {
@@ -105,7 +185,7 @@ func (logger *Logger) WithCaller(skip int) *Logger {
 	if _, file, line, ok := runtime.Caller(skip); ok {
 		return logger.
 			WithField("codeline", fmt.Sprintf("%s:%d", file, line))
-			//WithField("func", runtime.FuncForPC(pc).Name())
+		//WithField("func", runtime.FuncForPC(pc).Name())
 	}
 	return logger
 }
@@ -305,4 +385,4 @@ func (logger *Logger) isPrintable(s string) bool {
 		}
 	}
 	return true
-}
+}