Browse Source

Merge branch 'feature/change_log' of eta_server/eta_api into master

xyxie 1 year ago
parent
commit
daf834fa60
2 changed files with 16 additions and 13 deletions
  1. 3 0
      utils/config.go
  2. 13 13
      utils/logs.go

+ 3 - 0
utils/config.go

@@ -188,6 +188,7 @@ var (
 	BinLogFile  string
 	ApiLogPath  string //接口请求地址和接口返回值日志存放地址
 	ApiLogFile  string
+	LogMaxDays  int //日志最大保留天数
 )
 
 // MinIo配置
@@ -329,6 +330,8 @@ func init() {
 		BinLogFile = config["binlog_file"]
 		ApiLogPath = config["apilog_path"]
 		ApiLogFile = config["apilog_file"]
+		logMaxDaysStr := config["log_max_day"]
+		LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
 	}
 
 	// ES配置

+ 13 - 13
utils/logs.go

@@ -2,13 +2,11 @@ package utils
 
 import (
 	"encoding/json"
-	"fmt"
 	"github.com/beego/beego/v2/core/logs"
 	"github.com/sirupsen/logrus"
 	"gopkg.in/natefinch/lumberjack.v2"
 	"os"
 	"path"
-	"time"
 )
 
 const (
@@ -24,20 +22,22 @@ var FileLogData = logrus.New()
 var Binlog *logs.BeeLogger
 
 func init() {
+	if LogMaxDays == 0 {
+		LogMaxDays = 30
+	}
 	logPath := LogPath
 	if logPath == "" {
 		logPath = DefaultLogPath
 	}
 	logFile := LogFile
 	if logFile == "" {
-		logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
+		logFile = "filelog.log"
 	}
 	os.MkdirAll(logPath, os.ModePerm)
 
 	// 打开文件
 	logFileName := path.Join(logPath, logFile)
 	logConf := getDefaultLogrusConfig(logFileName)
-	logConf.MaxAge = 1
 	// 使用滚动压缩方式记录日志
 	rolling(FileLog, logConf)
 	//rolling(bLogFileName)
@@ -78,7 +78,7 @@ func initBinlog() {
 	}
 	binlogFile := BinLogFile
 	if binlogFile == "" {
-		binlogFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
+		binlogFile = "binlog.log"
 	}
 	os.MkdirAll(binlogPath, os.ModePerm)
 	logFileName := path.Join(binlogPath, binlogFile)
@@ -100,7 +100,7 @@ func initApiLog() {
 	}
 	logFile := ApiLogFile
 	if logFile == "" {
-		logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
+		logFile = "apilog.log"
 	}
 	os.MkdirAll(logPath, os.ModePerm)
 
@@ -124,7 +124,7 @@ func initFileLogData() {
 	}
 	logFile := LogDataFile
 	if logFile == "" {
-		logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
+		logFile = "datalog.log"
 	}
 	os.MkdirAll(logPath, os.ModePerm)
 
@@ -148,11 +148,11 @@ func rolling(fLog *logrus.Logger, config *lumberjack.Logger) {
 
 func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
 	config = &lumberjack.Logger{
-		Filename:   logFile, //日志文件位置
-		MaxSize:    256,     // 单文件最大容量,单位是MB
-		MaxBackups: 3,       // 最大保留过期文件个数
-		MaxAge:     7,       // 保留过期文件的最大时间间隔,单位是天
-		Compress:   true,    // 是否需要压缩滚动日志, 使用的 gzip 压缩
+		Filename:   logFile,    //日志文件位置
+		MaxSize:    256,        // 单文件最大容量,单位是MB
+		MaxBackups: 0,          // 最大保留过期文件个数,0表示默认保留
+		MaxAge:     LogMaxDays, // 保留过期文件的最大时间间隔,单位是天
+		Compress:   true,       // 是否需要压缩滚动日志, 使用的 gzip 压缩
 		LocalTime:  true,
 	}
 	return
@@ -164,7 +164,7 @@ func getDefaultLogConfig() logConfig {
 		MaxLines: 0,
 		MaxSize:  1 << 28,
 		Daily:    true,
-		MaxDays:  7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
+		MaxDays:  LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
 		Rotate:   true,
 		Level:    logs.LevelTrace,
 		//Perm:     "",