|
@@ -3,10 +3,11 @@ package utils
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
|
+ rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
|
"github.com/sirupsen/logrus"
|
|
|
- "gopkg.in/natefinch/lumberjack.v2"
|
|
|
"os"
|
|
|
"path"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
const (
|
|
@@ -37,10 +38,8 @@ func init() {
|
|
|
|
|
|
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
- logConf := getDefaultLogrusConfig(logFileName)
|
|
|
-
|
|
|
- rolling(FileLog, logConf)
|
|
|
-
|
|
|
+
|
|
|
+ rolling(FileLog, logFileName, LogMaxDays)
|
|
|
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
|
jsonFormat.DisableHTMLEscape = true
|
|
@@ -51,7 +50,6 @@ func init() {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
initBinlog()
|
|
|
initApiLog()
|
|
|
initFileLogData()
|
|
@@ -106,10 +104,8 @@ func initApiLog() {
|
|
|
|
|
|
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
- logConf := getDefaultLogrusConfig(logFileName)
|
|
|
-
|
|
|
- rolling(ApiLog, logConf)
|
|
|
-
|
|
|
+
|
|
|
+ rolling(ApiLog, logFileName, LogMaxDays)
|
|
|
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
|
jsonFormat.DisableHTMLEscape = true
|
|
@@ -130,9 +126,8 @@ func initFileLogData() {
|
|
|
|
|
|
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
- logConf := getDefaultLogrusConfig(logFileName)
|
|
|
-
|
|
|
- rolling(FileLogData, logConf)
|
|
|
+
|
|
|
+ rolling(FileLogData, logFileName, LogMaxDays)
|
|
|
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
|
jsonFormat.DisableHTMLEscape = true
|
|
@@ -140,24 +135,6 @@ func initFileLogData() {
|
|
|
FileLogData.SetFormatter(jsonFormat)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-func rolling(fLog *logrus.Logger, config *lumberjack.Logger) {
|
|
|
-
|
|
|
- fLog.SetOutput(config)
|
|
|
-}
|
|
|
-
|
|
|
-func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
|
|
|
- config = &lumberjack.Logger{
|
|
|
- Filename: logFile,
|
|
|
- MaxSize: 256,
|
|
|
- MaxBackups: 0,
|
|
|
- MaxAge: LogMaxDays,
|
|
|
- Compress: false,
|
|
|
- LocalTime: true,
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
func getDefaultLogConfig() logConfig {
|
|
|
return logConfig{
|
|
|
FileName: "",
|
|
@@ -170,3 +147,26 @@ func getDefaultLogConfig() logConfig {
|
|
|
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+func rolling(fLog *logrus.Logger, logFileName string, maxAge int) {
|
|
|
+
|
|
|
+ if maxAge == 0 {
|
|
|
+ maxAge = LogMaxDays
|
|
|
+ }
|
|
|
+ content, err := rotatelogs.New(
|
|
|
+ logFileName+"-%Y%m%d",
|
|
|
+
|
|
|
+ rotatelogs.WithClock(rotatelogs.Local),
|
|
|
+
|
|
|
+ rotatelogs.WithRotationSize(1<<28),
|
|
|
+ rotatelogs.WithMaxAge(time.Duration(maxAge*24)*time.Hour),
|
|
|
+
|
|
|
+ rotatelogs.WithRotationTime(24*time.Hour),
|
|
|
+ )
|
|
|
+ if err != nil {
|
|
|
+ panic("failed to create rotatelogs: " + err.Error())
|
|
|
+ }
|
|
|
+ fLog.SetOutput(content)
|
|
|
+ return
|
|
|
+}
|