Prechádzať zdrojové kódy

Merge branch 'feature/change_log' of eta_server/eta_task into master

xyxie 1 rok pred
rodič
commit
4c504affc8
2 zmenil súbory, kde vykonal 45 pridanie a 13 odobranie
  1. 1 1
      models/db.go
  2. 44 12
      utils/logs.go

+ 1 - 1
models/db.go

@@ -55,7 +55,7 @@ func init() {
 	etaDb.SetConnMaxLifetime(10 * time.Minute)
 	etaDb.SetConnMaxLifetime(10 * time.Minute)
 
 
 	orm.Debug = true
 	orm.Debug = true
-	orm.DebugLog = orm.NewLog(utils.Binlog.Writer())
+	orm.DebugLog = orm.NewLog(utils.Binlog)
 
 
 	// 注册Edb指标 数据表
 	// 注册Edb指标 数据表
 	initEdbDataTable()
 	initEdbDataTable()

+ 44 - 12
utils/logs.go

@@ -1,21 +1,22 @@
 package utils
 package utils
 
 
 import (
 import (
+	"encoding/json"
+	"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"
 )
 )
 
 
-var (
+const (
 	DefaultLogPath    = "./rdlucklog"
 	DefaultLogPath    = "./rdlucklog"
-	DefaultLogFile    = "eta_task.log"
 	DefaultBinlogPath = "./binlog"
 	DefaultBinlogPath = "./binlog"
 	DefaultBinlogFile = "binlog.log"
 	DefaultBinlogFile = "binlog.log"
 )
 )
 
 
 var FileLog = logrus.New()
 var FileLog = logrus.New()
-var Binlog = logrus.New()
+var Binlog *logs.BeeLogger
 
 
 func init() {
 func init() {
 	logPath := LogPath
 	logPath := LogPath
@@ -24,7 +25,7 @@ func init() {
 	}
 	}
 	logFile := LogFile
 	logFile := LogFile
 	if logFile == "" {
 	if logFile == "" {
-		logFile = DefaultLogFile
+		logFile = "eta_task.log"
 	}
 	}
 	os.MkdirAll(logPath, os.ModePerm)
 	os.MkdirAll(logPath, os.ModePerm)
 
 
@@ -36,7 +37,9 @@ func init() {
 	// 设置日志输出JSON格式
 	// 设置日志输出JSON格式
 	jsonFormat := new(logrus.JSONFormatter)
 	jsonFormat := new(logrus.JSONFormatter)
 	jsonFormat.DisableHTMLEscape = true
 	jsonFormat.DisableHTMLEscape = true
+	jsonFormat.TimestampFormat = HlbFormatDateTime
 	FileLog.SetFormatter(jsonFormat)
 	FileLog.SetFormatter(jsonFormat)
+	FileLog.SetReportCaller(true)
 	//LogInstance.SetFormatter(&logrus.TextFormatter{})
 	//LogInstance.SetFormatter(&logrus.TextFormatter{})
 	// 设置日志记录级别
 	// 设置日志记录级别
 	//FileLog.SetLevel(logrus.DebugLevel)
 	//FileLog.SetLevel(logrus.DebugLevel)
@@ -45,7 +48,20 @@ func init() {
 	initBinlog()
 	initBinlog()
 }
 }
 
 
+type logConfig struct {
+	FileName string `json:"filename" description:"保存的文件名"`
+	MaxLines int    `json:"maxlines"  description:"每个文件保存的最大行数,默认值 1000000"`
+	MaxSize  int    `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
+	Daily    bool   `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
+	MaxDays  int    `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
+	Rotate   bool   `json:"rotate" description:"是否开启 logrotate,默认是 true"`
+	Level    int    `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
+	Color    bool   `json:"color" description:"日志是否输出颜色"`
+	//Perm     string `json:"perm" description:"日志文件权限"`
+}
+
 func initBinlog() {
 func initBinlog() {
+	//binlog日志
 	//binlog日志
 	//binlog日志
 	binlogPath := BinLogPath
 	binlogPath := BinLogPath
 	if binlogPath == "" {
 	if binlogPath == "" {
@@ -56,14 +72,16 @@ func initBinlog() {
 		binlogFile = DefaultBinlogFile
 		binlogFile = DefaultBinlogFile
 	}
 	}
 	os.MkdirAll(binlogPath, os.ModePerm)
 	os.MkdirAll(binlogPath, os.ModePerm)
-	// 使用滚动压缩方式记录日志
 	logFileName := path.Join(binlogPath, binlogFile)
 	logFileName := path.Join(binlogPath, binlogFile)
-	rolling(Binlog, logFileName)
-	//rolling(bLogFileName)
-	// 设置日志输出JSON格式
-	jsonFormat := new(logrus.JSONFormatter)
-	jsonFormat.DisableHTMLEscape = true
-	Binlog.SetFormatter(jsonFormat)
+	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)
 }
 }
 
 
 // 日志滚动设置
 // 日志滚动设置
@@ -71,9 +89,23 @@ func rolling(fLog *logrus.Logger, logFile string) {
 	// 设置输出
 	// 设置输出
 	fLog.SetOutput(&lumberjack.Logger{
 	fLog.SetOutput(&lumberjack.Logger{
 		Filename:   logFile, //日志文件位置
 		Filename:   logFile, //日志文件位置
-		MaxSize:    500,     // 单文件最大容量,单位是MB
+		MaxSize:    100,     // 单文件最大容量,单位是MB
 		MaxBackups: 3,       // 最大保留过期文件个数
 		MaxBackups: 3,       // 最大保留过期文件个数
 		MaxAge:     7,       // 保留过期文件的最大时间间隔,单位是天
 		MaxAge:     7,       // 保留过期文件的最大时间间隔,单位是天
 		Compress:   true,    // 是否需要压缩滚动日志, 使用的 gzip 压缩
 		Compress:   true,    // 是否需要压缩滚动日志, 使用的 gzip 压缩
+		LocalTime:  true,
 	})
 	})
 }
 }
+
+func getDefaultLogConfig() logConfig {
+	return logConfig{
+		FileName: "",
+		MaxLines: 0,
+		MaxSize:  1 << 28,
+		Daily:    true,
+		MaxDays:  7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
+		Rotate:   true,
+		Level:    logs.LevelTrace,
+		//Perm:     "",
+	}
+}