|
@@ -2,17 +2,20 @@ 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 (
|
|
|
- DefaultLogPath = "./rdlucklog"
|
|
|
- DefaultBinlogPath = "./binlog"
|
|
|
- DefaultBinlogFile = "binlog.log"
|
|
|
+ DefaultLogPath = "./rdlucklog/filelog"
|
|
|
+ DefaultBinlogPath = "./rdlucklog/binlog"
|
|
|
+ DefaultDataPath = "./rdlucklog/filedata"
|
|
|
+ DefaultApiLogPath = "./rdlucklog/apilog"
|
|
|
)
|
|
|
|
|
|
var FileLog = logrus.New()
|
|
@@ -27,14 +30,16 @@ func init() {
|
|
|
}
|
|
|
logFile := LogFile
|
|
|
if logFile == "" {
|
|
|
- logFile = "eta_api.log"
|
|
|
+ logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDate))
|
|
|
}
|
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
// 打开文件
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
+ logConf := getDefaultLogrusConfig(logFileName)
|
|
|
+ logConf.MaxAge = 1
|
|
|
// 使用滚动压缩方式记录日志
|
|
|
- rolling(FileLog, logFileName)
|
|
|
+ rolling(FileLog, logConf)
|
|
|
//rolling(bLogFileName)
|
|
|
// 设置日志输出JSON格式
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
@@ -73,7 +78,7 @@ func initBinlog() {
|
|
|
}
|
|
|
binlogFile := BinLogFile
|
|
|
if binlogFile == "" {
|
|
|
- binlogFile = DefaultBinlogFile
|
|
|
+ binlogFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDate))
|
|
|
}
|
|
|
os.MkdirAll(binlogPath, os.ModePerm)
|
|
|
logFileName := path.Join(binlogPath, binlogFile)
|
|
@@ -91,22 +96,19 @@ func initBinlog() {
|
|
|
func initApiLog() {
|
|
|
logPath := ApiLogPath
|
|
|
if logPath == "" {
|
|
|
- if RunMode == "release" {
|
|
|
- logPath = `/data/etalogs/eta_api`
|
|
|
- } else {
|
|
|
- logPath = `./rdlucklog/api`
|
|
|
- }
|
|
|
+ logPath = DefaultApiLogPath
|
|
|
}
|
|
|
logFile := ApiLogFile
|
|
|
if logFile == "" {
|
|
|
- logFile = "eta_api_api.log"
|
|
|
+ logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDate))
|
|
|
}
|
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
// 打开文件
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
+ logConf := getDefaultLogrusConfig(logFileName)
|
|
|
// 使用滚动压缩方式记录日志
|
|
|
- rolling(ApiLog, logFileName)
|
|
|
+ rolling(ApiLog, logConf)
|
|
|
//rolling(bLogFileName)
|
|
|
// 设置日志输出JSON格式
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
@@ -118,19 +120,19 @@ func initApiLog() {
|
|
|
func initFileLogData() {
|
|
|
logPath := LogDataPath
|
|
|
if logPath == "" {
|
|
|
- logPath = DefaultLogPath
|
|
|
+ logPath = DefaultDataPath
|
|
|
}
|
|
|
logFile := LogDataFile
|
|
|
if logFile == "" {
|
|
|
- logFile = "eta_api_data.log"
|
|
|
+ logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDate))
|
|
|
}
|
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
// 打开文件
|
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
+ logConf := getDefaultLogrusConfig(logFileName)
|
|
|
// 使用滚动压缩方式记录日志
|
|
|
- rolling(FileLogData, logFileName)
|
|
|
- //rolling(bLogFileName)
|
|
|
+ rolling(FileLogData, logConf)
|
|
|
// 设置日志输出JSON格式
|
|
|
jsonFormat := new(logrus.JSONFormatter)
|
|
|
jsonFormat.DisableHTMLEscape = true
|
|
@@ -139,16 +141,21 @@ func initFileLogData() {
|
|
|
}
|
|
|
|
|
|
// 日志滚动设置
|
|
|
-func rolling(fLog *logrus.Logger, logFile string) {
|
|
|
+func rolling(fLog *logrus.Logger, config *lumberjack.Logger) {
|
|
|
// 设置输出
|
|
|
- fLog.SetOutput(&lumberjack.Logger{
|
|
|
+ fLog.SetOutput(config)
|
|
|
+}
|
|
|
+
|
|
|
+func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
|
|
|
+ config = &lumberjack.Logger{
|
|
|
Filename: logFile, //日志文件位置
|
|
|
- MaxSize: 100, // 单文件最大容量,单位是MB
|
|
|
+ MaxSize: 256, // 单文件最大容量,单位是MB
|
|
|
MaxBackups: 3, // 最大保留过期文件个数
|
|
|
MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
|
|
|
Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
|
|
|
LocalTime: true,
|
|
|
- })
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
func getDefaultLogConfig() logConfig {
|