|
@@ -2,13 +2,11 @@ package utils
|
|
|
|
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
- "fmt"
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
"github.com/beego/beego/v2/core/logs"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/sirupsen/logrus"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
"os"
|
|
"os"
|
|
"path"
|
|
"path"
|
|
- "time"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
const (
|
|
const (
|
|
@@ -22,20 +20,22 @@ var ApiLog = logrus.New()
|
|
var Binlog *logs.BeeLogger
|
|
var Binlog *logs.BeeLogger
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
|
|
+ if LogMaxDays == 0 {
|
|
|
|
+ LogMaxDays = 30
|
|
|
|
+ }
|
|
logPath := LogPath
|
|
logPath := LogPath
|
|
if logPath == "" {
|
|
if logPath == "" {
|
|
logPath = DefaultLogPath
|
|
logPath = DefaultLogPath
|
|
}
|
|
}
|
|
logFile := LogFile
|
|
logFile := LogFile
|
|
if logFile == "" {
|
|
if logFile == "" {
|
|
- logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
|
|
|
|
|
|
+ logFile = "filelog.log"
|
|
}
|
|
}
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
// 打开文件
|
|
// 打开文件
|
|
logFileName := path.Join(logPath, logFile)
|
|
logFileName := path.Join(logPath, logFile)
|
|
logConf := getDefaultLogrusConfig(logFileName)
|
|
logConf := getDefaultLogrusConfig(logFileName)
|
|
- logConf.MaxAge = 1
|
|
|
|
// 使用滚动压缩方式记录日志
|
|
// 使用滚动压缩方式记录日志
|
|
rolling(FileLog, logConf)
|
|
rolling(FileLog, logConf)
|
|
//rolling(bLogFileName)
|
|
//rolling(bLogFileName)
|
|
@@ -49,7 +49,6 @@ func init() {
|
|
// 设置日志记录级别
|
|
// 设置日志记录级别
|
|
//FileLog.SetLevel(logrus.DebugLevel)
|
|
//FileLog.SetLevel(logrus.DebugLevel)
|
|
|
|
|
|
- //初始化binlog日志
|
|
|
|
//FileLog.Info("abc")
|
|
//FileLog.Info("abc")
|
|
initBinlog()
|
|
initBinlog()
|
|
initApiLog()
|
|
initApiLog()
|
|
@@ -76,7 +75,7 @@ func initBinlog() {
|
|
}
|
|
}
|
|
binlogFile := BinLogFile
|
|
binlogFile := BinLogFile
|
|
if binlogFile == "" {
|
|
if binlogFile == "" {
|
|
- binlogFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
|
|
|
|
|
|
+ binlogFile = "binlog.log"
|
|
}
|
|
}
|
|
os.MkdirAll(binlogPath, os.ModePerm)
|
|
os.MkdirAll(binlogPath, os.ModePerm)
|
|
logFileName := path.Join(binlogPath, binlogFile)
|
|
logFileName := path.Join(binlogPath, binlogFile)
|
|
@@ -98,7 +97,7 @@ func initApiLog() {
|
|
}
|
|
}
|
|
logFile := ApiLogFile
|
|
logFile := ApiLogFile
|
|
if logFile == "" {
|
|
if logFile == "" {
|
|
- logFile = fmt.Sprintf("%s.log", time.Now().Format(FormatDateUnSpace))
|
|
|
|
|
|
+ logFile = "apilog.log"
|
|
}
|
|
}
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
@@ -123,11 +122,11 @@ func rolling(fLog *logrus.Logger, config *lumberjack.Logger) {
|
|
|
|
|
|
func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
|
|
func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
|
|
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, // 最大保留过期文件个数
|
|
|
|
+ MaxAge: LogMaxDays, // 保留过期文件的最大时间间隔,单位是天
|
|
|
|
+ Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
|
|
LocalTime: true,
|
|
LocalTime: true,
|
|
}
|
|
}
|
|
return
|
|
return
|
|
@@ -139,7 +138,7 @@ func getDefaultLogConfig() logConfig {
|
|
MaxLines: 0,
|
|
MaxLines: 0,
|
|
MaxSize: 1 << 28,
|
|
MaxSize: 1 << 28,
|
|
Daily: true,
|
|
Daily: true,
|
|
- MaxDays: 7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
|
|
|
|
|
|
+ MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
|
|
Rotate: true,
|
|
Rotate: true,
|
|
Level: logs.LevelTrace,
|
|
Level: logs.LevelTrace,
|
|
//Perm: "",
|
|
//Perm: "",
|