瀏覽代碼

Merge remote-tracking branch 'origin/master'

Roc 1 年之前
父節點
當前提交
3e5e43a8a2
共有 3 個文件被更改,包括 27 次插入16 次删除
  1. 1 0
      .gitignore
  2. 3 0
      utils/config.go
  3. 23 16
      utils/logs.go

+ 1 - 0
.gitignore

@@ -1,6 +1,7 @@
 /.idea
 /hz_eta_data.exe
 /rdlucklog
+/etalogs
 /binlog
 /conf
 /*.exe

+ 3 - 0
utils/config.go

@@ -33,6 +33,7 @@ var (
 	LogFile    string
 	BinLogPath string //数据库相关的日志存放地址
 	BinLogFile string
+	LogMaxDays int //日志最大保留天数
 )
 
 func init() {
@@ -69,6 +70,8 @@ func init() {
 		LogFile = config["log_file"]
 		BinLogPath = config["binlog_path"]
 		BinLogFile = config["binlog_file"]
+		logMaxDaysStr := config["log_max_day"]
+		LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
 	}
 }
 

+ 23 - 16
utils/logs.go

@@ -10,29 +10,32 @@ import (
 )
 
 const (
-	DefaultLogPath    = "./rdlucklog"
-	DefaultBinlogPath = "./binlog"
-	DefaultBinlogFile = "binlog.log"
+	DefaultLogPath    = "./etalogs/filelog"
+	DefaultBinlogPath = "./etalogs/binlog"
 )
 
 var FileLog = logrus.New()
 var Binlog *logs.BeeLogger
 
 func init() {
+	if LogMaxDays == 0 {
+		LogMaxDays = 30
+	}
 	logPath := LogPath
 	if logPath == "" {
 		logPath = DefaultLogPath
 	}
 	logFile := LogFile
 	if logFile == "" {
-		logFile = "eta_data_init.log"
+		logFile = "filelog.log"
 	}
 	os.MkdirAll(logPath, os.ModePerm)
 
 	// 打开文件
 	logFileName := path.Join(logPath, logFile)
+	logConf := getDefaultLogrusConfig(logFileName)
 	// 使用滚动压缩方式记录日志
-	rolling(FileLog, logFileName)
+	rolling(FileLog, logConf)
 	//rolling(bLogFileName)
 	// 设置日志输出JSON格式
 	jsonFormat := new(logrus.JSONFormatter)
@@ -61,7 +64,6 @@ type logConfig struct {
 }
 
 func initBinlog() {
-	//binlog日志
 	//binlog日志
 	binlogPath := BinLogPath
 	if binlogPath == "" {
@@ -69,7 +71,7 @@ func initBinlog() {
 	}
 	binlogFile := BinLogFile
 	if binlogFile == "" {
-		binlogFile = DefaultBinlogFile
+		binlogFile = "binlog.log"
 	}
 	os.MkdirAll(binlogPath, os.ModePerm)
 	logFileName := path.Join(binlogPath, binlogFile)
@@ -85,16 +87,21 @@ func initBinlog() {
 }
 
 // 日志滚动设置
-func rolling(fLog *logrus.Logger, logFile string) {
+func rolling(fLog *logrus.Logger, config *lumberjack.Logger) {
 	// 设置输出
-	fLog.SetOutput(&lumberjack.Logger{
-		Filename:   logFile, //日志文件位置
-		MaxSize:    100,     // 单文件最大容量,单位是MB
-		MaxBackups: 3,       // 最大保留过期文件个数
-		MaxAge:     7,       // 保留过期文件的最大时间间隔,单位是天
-		Compress:   true,    // 是否需要压缩滚动日志, 使用的 gzip 压缩
+	fLog.SetOutput(config)
+}
+
+func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
+	config = &lumberjack.Logger{
+		Filename:   logFile,    //日志文件位置
+		MaxSize:    256,        // 单文件最大容量,单位是MB
+		MaxBackups: 0,          // 最大保留过期文件个数
+		MaxAge:     LogMaxDays, // 保留过期文件的最大时间间隔,单位是天
+		Compress:   true,       // 是否需要压缩滚动日志, 使用的 gzip 压缩
 		LocalTime:  true,
-	})
+	}
+	return
 }
 
 func getDefaultLogConfig() logConfig {
@@ -103,7 +110,7 @@ func getDefaultLogConfig() logConfig {
 		MaxLines: 0,
 		MaxSize:  1 << 28,
 		Daily:    true,
-		MaxDays:  7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
+		MaxDays:  LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
 		Rotate:   true,
 		Level:    logs.LevelTrace,
 		//Perm:     "",