|
@@ -3,32 +3,49 @@ package utils
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
|
+ "github.com/sirupsen/logrus"
|
|
|
+ "gopkg.in/natefinch/lumberjack.v2"
|
|
|
"os"
|
|
|
+ "path"
|
|
|
)
|
|
|
|
|
|
-var FileLog *logs.BeeLogger
|
|
|
+const (
|
|
|
+ DefaultLogPath = "./rdlucklog"
|
|
|
+ DefaultBinlogPath = "./binlog"
|
|
|
+ DefaultBinlogFile = "binlog.log"
|
|
|
+)
|
|
|
+
|
|
|
+var FileLog = logrus.New()
|
|
|
var Binlog *logs.BeeLogger
|
|
|
|
|
|
func init() {
|
|
|
- FileLog = logs.NewLogger(1000000)
|
|
|
- FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/eta_data_init.log"}`)
|
|
|
- FileLog.EnableFuncCallDepth(true)
|
|
|
+ logPath := LogPath
|
|
|
+ if logPath == "" {
|
|
|
+ logPath = DefaultLogPath
|
|
|
+ }
|
|
|
+ logFile := LogFile
|
|
|
+ if logFile == "" {
|
|
|
+ logFile = "eta_data_init.log"
|
|
|
+ }
|
|
|
+ os.MkdirAll(logPath, os.ModePerm)
|
|
|
|
|
|
- initBinlog()
|
|
|
-}
|
|
|
+ // 打开文件
|
|
|
+ logFileName := path.Join(logPath, logFile)
|
|
|
+ // 使用滚动压缩方式记录日志
|
|
|
+ rolling(FileLog, logFileName)
|
|
|
+ //rolling(bLogFileName)
|
|
|
+ // 设置日志输出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)
|
|
|
|
|
|
-func initBinlog() {
|
|
|
- //binlog日志
|
|
|
- binLogDir := `./binlog`
|
|
|
- os.MkdirAll(binLogDir, os.ModePerm)
|
|
|
- Binlog = logs.NewLogger(1000000)
|
|
|
- logConfig := getDefaultLogConfig()
|
|
|
- logConfig.FileName = "./binlog/binlog.log"
|
|
|
- logConfig.MaxLines = 10000000
|
|
|
- logConfig.Rotate = true
|
|
|
- b, _ := json.Marshal(logConfig)
|
|
|
- Binlog.SetLogger(logs.AdapterFile, string(b))
|
|
|
- Binlog.EnableFuncCallDepth(true)
|
|
|
+ //FileLog.Info("abc")
|
|
|
+ initBinlog()
|
|
|
}
|
|
|
|
|
|
type logConfig struct {
|
|
@@ -43,13 +60,50 @@ type logConfig struct {
|
|
|
//Perm string `json:"perm" description:"日志文件权限"`
|
|
|
}
|
|
|
|
|
|
+func initBinlog() {
|
|
|
+ //binlog日志
|
|
|
+ //binlog日志
|
|
|
+ binlogPath := BinLogPath
|
|
|
+ if binlogPath == "" {
|
|
|
+ binlogPath = DefaultBinlogPath
|
|
|
+ }
|
|
|
+ binlogFile := BinLogFile
|
|
|
+ if binlogFile == "" {
|
|
|
+ binlogFile = DefaultBinlogFile
|
|
|
+ }
|
|
|
+ os.MkdirAll(binlogPath, os.ModePerm)
|
|
|
+ logFileName := path.Join(binlogPath, binlogFile)
|
|
|
+ Binlog = logs.NewLogger(1000000)
|
|
|
+ logConf := getDefaultLogConfig()
|
|
|
+
|
|
|
+ logConf.FileName = logFileName
|
|
|
+ logConf.MaxLines = 10000000
|
|
|
+ logConf.Rotate = true
|
|
|
+ b, _ := json.Marshal(logConf)
|
|
|
+ Binlog.SetLogger(logs.AdapterFile, string(b))
|
|
|
+ Binlog.EnableFuncCallDepth(true)
|
|
|
+}
|
|
|
+
|
|
|
+// 日志滚动设置
|
|
|
+func rolling(fLog *logrus.Logger, logFile string) {
|
|
|
+ // 设置输出
|
|
|
+ fLog.SetOutput(&lumberjack.Logger{
|
|
|
+ Filename: logFile, //日志文件位置
|
|
|
+ MaxSize: 100, // 单文件最大容量,单位是MB
|
|
|
+ MaxBackups: 3, // 最大保留过期文件个数
|
|
|
+ MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
|
|
|
+ Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
|
|
|
+ LocalTime: true,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
func getDefaultLogConfig() logConfig {
|
|
|
return logConfig{
|
|
|
FileName: "",
|
|
|
MaxLines: 0,
|
|
|
MaxSize: 1 << 28,
|
|
|
Daily: true,
|
|
|
- MaxDays: 31, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
|
|
|
+ MaxDays: 7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
|
|
|
Rotate: true,
|
|
|
Level: logs.LevelTrace,
|
|
|
//Perm: "",
|