|
@@ -3,11 +3,8 @@ package utils
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"github.com/beego/beego/v2/core/logs"
|
|
"github.com/beego/beego/v2/core/logs"
|
|
- rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
|
|
- "github.com/sirupsen/logrus"
|
|
|
|
"os"
|
|
"os"
|
|
"path"
|
|
"path"
|
|
- "time"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
const (
|
|
const (
|
|
@@ -17,9 +14,9 @@ const (
|
|
DefaultApiLogPath = "./etalogs/apilog"
|
|
DefaultApiLogPath = "./etalogs/apilog"
|
|
)
|
|
)
|
|
|
|
|
|
-var FileLog = logrus.New()
|
|
|
|
-var ApiLog = logrus.New()
|
|
|
|
-var FileLogData = logrus.New()
|
|
|
|
|
|
+var FileLog *logs.BeeLogger
|
|
|
|
+var ApiLog *logs.BeeLogger
|
|
|
|
+var FileLogData *logs.BeeLogger
|
|
var Binlog *logs.BeeLogger
|
|
var Binlog *logs.BeeLogger
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
@@ -38,17 +35,13 @@ func init() {
|
|
|
|
|
|
// 打开文件
|
|
// 打开文件
|
|
logFileName := path.Join(logPath, logFile)
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
|
+ FileLog = logs.NewLogger(1000000)
|
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
|
|
|
- rolling(FileLog, logFileName, LogMaxDays)
|
|
|
|
- // 设置日志输出JSON格式
|
|
|
|
- jsonFormat := new(logrus.JSONFormatter)
|
|
|
|
- jsonFormat.DisableHTMLEscape = true
|
|
|
|
- jsonFormat.TimestampFormat = HlbFormatDateTime
|
|
|
|
- FileLog.SetFormatter(jsonFormat)
|
|
|
|
- FileLog.SetReportCaller(true)
|
|
|
|
- //LogInstance.SetFormatter(&logrus.TextFormatter{})
|
|
|
|
- // 设置日志记录级别
|
|
|
|
- //FileLog.SetLevel(logrus.DebugLevel)
|
|
|
|
|
|
+ logConf.FileName = logFileName
|
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
|
+ FileLog.SetLogger(logs.AdapterFile, string(b))
|
|
|
|
+ FileLog.EnableFuncCallDepth(true)
|
|
|
|
|
|
initBinlog()
|
|
initBinlog()
|
|
initApiLog()
|
|
initApiLog()
|
|
@@ -84,8 +77,8 @@ func initBinlog() {
|
|
logConf := getDefaultLogConfig()
|
|
logConf := getDefaultLogConfig()
|
|
|
|
|
|
logConf.FileName = logFileName
|
|
logConf.FileName = logFileName
|
|
- logConf.MaxLines = 10000000
|
|
|
|
- logConf.Rotate = true
|
|
|
|
|
|
+ //logConf.MaxLines = 10000000
|
|
|
|
+ //logConf.Rotate = true
|
|
b, _ := json.Marshal(logConf)
|
|
b, _ := json.Marshal(logConf)
|
|
Binlog.SetLogger(logs.AdapterFile, string(b))
|
|
Binlog.SetLogger(logs.AdapterFile, string(b))
|
|
Binlog.EnableFuncCallDepth(true)
|
|
Binlog.EnableFuncCallDepth(true)
|
|
@@ -104,13 +97,13 @@ func initApiLog() {
|
|
|
|
|
|
// 打开文件
|
|
// 打开文件
|
|
logFileName := path.Join(logPath, logFile)
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
|
+ ApiLog = logs.NewLogger(1000000)
|
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
|
|
|
- rolling(ApiLog, logFileName, LogMaxDays)
|
|
|
|
- // 设置日志输出JSON格式
|
|
|
|
- jsonFormat := new(logrus.JSONFormatter)
|
|
|
|
- jsonFormat.DisableHTMLEscape = true
|
|
|
|
- jsonFormat.TimestampFormat = HlbFormatDateTime
|
|
|
|
- ApiLog.SetFormatter(jsonFormat)
|
|
|
|
|
|
+ logConf.FileName = logFileName
|
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
|
+ ApiLog.SetLogger(logs.AdapterFile, string(b))
|
|
|
|
+ ApiLog.EnableFuncCallDepth(true)
|
|
}
|
|
}
|
|
|
|
|
|
func initFileLogData() {
|
|
func initFileLogData() {
|
|
@@ -126,19 +119,19 @@ func initFileLogData() {
|
|
|
|
|
|
// 打开文件
|
|
// 打开文件
|
|
logFileName := path.Join(logPath, logFile)
|
|
logFileName := path.Join(logPath, logFile)
|
|
|
|
+ FileLogData = logs.NewLogger(1000000)
|
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
|
|
|
- rolling(FileLogData, logFileName, LogMaxDays)
|
|
|
|
- // 设置日志输出JSON格式
|
|
|
|
- jsonFormat := new(logrus.JSONFormatter)
|
|
|
|
- jsonFormat.DisableHTMLEscape = true
|
|
|
|
- jsonFormat.TimestampFormat = HlbFormatDateTime
|
|
|
|
- FileLogData.SetFormatter(jsonFormat)
|
|
|
|
|
|
+ logConf.FileName = logFileName
|
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
|
+ FileLogData.SetLogger(logs.AdapterFile, string(b))
|
|
|
|
+ FileLogData.EnableFuncCallDepth(true)
|
|
}
|
|
}
|
|
|
|
|
|
func getDefaultLogConfig() logConfig {
|
|
func getDefaultLogConfig() logConfig {
|
|
return logConfig{
|
|
return logConfig{
|
|
FileName: "",
|
|
FileName: "",
|
|
- MaxLines: 0,
|
|
|
|
|
|
+ MaxLines: 10000000,
|
|
MaxSize: 1 << 28,
|
|
MaxSize: 1 << 28,
|
|
Daily: true,
|
|
Daily: true,
|
|
MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
|
|
MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
|
|
@@ -147,26 +140,3 @@ func getDefaultLogConfig() logConfig {
|
|
//Perm: "",
|
|
//Perm: "",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-// 日志滚动设置
|
|
|
|
-func rolling(fLog *logrus.Logger, logFileName string, maxAge int) {
|
|
|
|
- // 设置输出
|
|
|
|
- if maxAge == 0 {
|
|
|
|
- maxAge = LogMaxDays
|
|
|
|
- }
|
|
|
|
- content, err := rotatelogs.New(
|
|
|
|
- logFileName+"-%Y%m%d",
|
|
|
|
- //rotatelogs.WithLinkName("./log/cli.log"), // 生成软链,指向最新日志文件
|
|
|
|
- rotatelogs.WithClock(rotatelogs.Local),
|
|
|
|
- //MaxAge and RotationCount cannot be both set 两者不能同时设置
|
|
|
|
- rotatelogs.WithRotationSize(1<<28), //256mb
|
|
|
|
- rotatelogs.WithMaxAge(time.Duration(maxAge*24)*time.Hour), //clear
|
|
|
|
- //rotatelogs.WithRotationCount(5), //number 默认7份 大于7份 或到了清理时间 开始清理
|
|
|
|
- rotatelogs.WithRotationTime(24*time.Hour), //rotate 最小为1分钟轮询。默认60s 低于1分钟就按1分钟来
|
|
|
|
- )
|
|
|
|
- if err != nil {
|
|
|
|
- panic("failed to create rotatelogs: " + err.Error())
|
|
|
|
- }
|
|
|
|
- fLog.SetOutput(content)
|
|
|
|
- return
|
|
|
|
-}
|
|
|