Bladeren bron

重置binlog

xyxie 1 jaar geleden
bovenliggende
commit
f1d1580cb3
4 gewijzigde bestanden met toevoegingen van 44 en 13 verwijderingen
  1. 1 1
      go.mod
  2. 2 2
      go.sum
  3. 1 1
      models/db.go
  4. 40 9
      utils/logs.go

+ 1 - 1
go.mod

@@ -26,7 +26,7 @@ require (
 	github.com/rdlucklib/rdluck_tools v1.0.3
 	github.com/shopspring/decimal v1.3.1
 	github.com/silenceper/wechat/v2 v2.1.3
-	github.com/sirupsen/logrus v1.9.0
+	github.com/sirupsen/logrus v1.9.3
 	github.com/tealeg/xlsx v1.0.5
 	github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.541
 	github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses v1.0.541

+ 2 - 2
go.sum

@@ -366,8 +366,8 @@ github.com/silenceper/wechat/v2 v2.1.3/go.mod h1:FoU0YvegD+Z85TBGQhjkXjY8BMb0+ca
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
 github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
 github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
-github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
-github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
+github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
+github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
 github.com/smartystreets/assertions v1.1.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
 github.com/smartystreets/assertions v1.1.1/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=

+ 1 - 1
models/db.go

@@ -66,7 +66,7 @@ func init() {
 	}
 
 	orm.Debug = true
-	orm.DebugLog = orm.NewLog(utils.Binlog.Writer())
+	orm.DebugLog = orm.NewLog(utils.Binlog)
 
 	//注册对象
 	orm.RegisterModel(

+ 40 - 9
utils/logs.go

@@ -1,6 +1,8 @@
 package utils
 
 import (
+	"encoding/json"
+	"github.com/beego/beego/v2/core/logs"
 	"github.com/sirupsen/logrus"
 	"gopkg.in/natefinch/lumberjack.v2"
 	"os"
@@ -14,9 +16,9 @@ const (
 )
 
 var FileLog = logrus.New()
-var Binlog = logrus.New()
 var ApiLog = logrus.New()
 var FileLogData = logrus.New()
+var Binlog *logs.BeeLogger
 
 func init() {
 	logPath := LogPath
@@ -47,9 +49,23 @@ func init() {
 	//FileLog.Info("abc")
 	initBinlog()
 	initApiLog()
+	initFileLogData()
+}
+
+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() {
+	//binlog日志
 	//binlog日志
 	binlogPath := BinLogPath
 	if binlogPath == "" {
@@ -60,16 +76,18 @@ func initBinlog() {
 		binlogFile = DefaultBinlogFile
 	}
 	os.MkdirAll(binlogPath, os.ModePerm)
-	// 使用滚动压缩方式记录日志
 	logFileName := path.Join(binlogPath, binlogFile)
-	rolling(Binlog, logFileName)
-	//rolling(bLogFileName)
-	// 设置日志输出JSON格式
-	jsonFormat := new(logrus.JSONFormatter)
-	jsonFormat.DisableHTMLEscape = true
-	jsonFormat.TimestampFormat = HlbFormatDateTime
-	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)
 }
+
 func initApiLog() {
 	logPath := ApiLogPath
 	if logPath == "" {
@@ -132,3 +150,16 @@ func rolling(fLog *logrus.Logger, logFile string) {
 		LocalTime:  true,
 	})
 }
+
+func getDefaultLogConfig() logConfig {
+	return logConfig{
+		FileName: "",
+		MaxLines: 0,
+		MaxSize:  1 << 28,
+		Daily:    true,
+		MaxDays:  7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
+		Rotate:   true,
+		Level:    logs.LevelTrace,
+		//Perm:     "",
+	}
+}