Browse Source

日志修改

xyxie 1 year ago
parent
commit
e15e4ab3bd
5 changed files with 79 additions and 42 deletions
  1. 3 0
      go.mod
  2. 4 0
      go.sum
  3. 1 1
      models/db.go
  4. 13 0
      utils/config.go
  5. 58 41
      utils/logs.go

+ 3 - 0
go.mod

@@ -13,9 +13,11 @@ require (
 	github.com/olivere/elastic/v7 v7.0.32
 	github.com/rdlucklib/rdluck_tools v1.0.3
 	github.com/shopspring/decimal v1.3.1
+	github.com/sirupsen/logrus v1.6.0
 	github.com/yidane/formula v0.0.0-20210902154546-0782e1736717
 	golang.org/x/net v0.7.0
 	gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
+	gopkg.in/natefinch/lumberjack.v2 v2.2.1
 )
 
 require (
@@ -26,6 +28,7 @@ require (
 	github.com/golang/protobuf v1.5.2 // indirect
 	github.com/hashicorp/golang-lru v0.5.4 // indirect
 	github.com/josharian/intern v1.0.0 // indirect
+	github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
 	github.com/mailru/easyjson v0.7.7 // indirect
 	github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
 	github.com/mitchellh/mapstructure v1.4.1 // indirect

+ 4 - 0
go.sum

@@ -215,6 +215,7 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
 github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
 github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
+github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
 github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
 github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -354,6 +355,7 @@ github.com/siddontang/goredis v0.0.0-20150324035039-760763f78400/go.mod h1:DDcKz
 github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d/go.mod h1:AMEsy7v5z92TR1JKMkLLoaOQk++LVnOKL3ScbJ8GNGA=
 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.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
 github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
 github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
@@ -594,6 +596,8 @@ gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
 gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
+gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
+gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
 gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
 gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=

+ 1 - 1
models/db.go

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

+ 13 - 0
utils/config.go

@@ -16,6 +16,13 @@ var (
 	MYSQL_URL_ETA  string
 )
 
+var (
+	LogPath    string
+	LogFile    string
+	BinLogPath string
+	BinLogFile string
+)
+
 var SystemType string // 系统类型; hz:弘则;trial:试用平台;custom:客户
 
 var (
@@ -149,6 +156,12 @@ func init() {
 	}
 	APPNAME = appNameCn
 
+	{
+		LogPath = config["log_path"]
+		LogFile = config["log_file"]
+		BinLogPath = config["binlog_path"]
+		BinLogFile = config["binlog_file"]
+	}
 	// 邮件提醒人员
 	EmailSendToUsers = config["email_send_to_users"]
 

+ 58 - 41
utils/logs.go

@@ -1,56 +1,73 @@
 package utils
 
 import (
-	"encoding/json"
-	"github.com/beego/beego/v2/adapter/logs"
-	"os"
+	"github.com/sirupsen/logrus"
+	"gopkg.in/natefinch/lumberjack.v2"
+	"path"
 )
 
-var FileLog *logs.BeeLogger
-var Binlog *logs.BeeLogger
+var (
+	DefaultLogPath    = "./rdlucklog"
+	DefaultLogFile    = "eta_task.log"
+	DefaultBinlogPath = "./binlog"
+	DefaultBinlogFile = "binlog.log"
+)
+
+var FileLog = logrus.New()
+var Binlog = logrus.New()
 
 func init() {
-	FileLog = logs.NewLogger(1000000)
-	FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/eta_task.log"}`)
-	//初始化binlog日志
+	logPath := LogPath
+	if logPath == "" {
+		logPath = DefaultLogPath
+	}
+	logFile := LogFile
+	if logFile == "" {
+		logFile = DefaultLogFile
+	}
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	// 使用滚动压缩方式记录日志
+	rolling(FileLog, logFileName)
+	//rolling(bLogFileName)
+	// 设置日志输出JSON格式
+	FileLog.SetFormatter(&logrus.JSONFormatter{})
+	//LogInstance.SetFormatter(&logrus.TextFormatter{})
+	// 设置日志记录级别
+	//FileLog.SetLevel(logrus.DebugLevel)
+
+	//FileLog.Info("abc")
 	initBinlog()
 }
 
 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)
-}
-
-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:"日志文件权限"`
+	binlogPath := BinLogPath
+	if binlogPath == "" {
+		binlogPath = DefaultBinlogPath
+	}
+	binlogFile := BinLogFile
+	if binlogFile == "" {
+		binlogFile = DefaultBinlogFile
+	}
+	// 使用滚动压缩方式记录日志
+	logFileName := path.Join(binlogPath, binlogFile)
+	rolling(Binlog, logFileName)
+	//rolling(bLogFileName)
+	// 设置日志输出JSON格式
+	jsonFormat := new(logrus.JSONFormatter)
+	jsonFormat.DisableHTMLEscape = true
+	Binlog.SetFormatter(jsonFormat)
 }
 
-func getDefaultLogConfig() logConfig {
-	return logConfig{
-		FileName: "",
-		MaxLines: 0,
-		MaxSize:  1 << 28,
-		Daily:    true,
-		MaxDays:  31, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
-		Rotate:   true,
-		Level:    logs.LevelTrace,
-		//Perm:     "",
-	}
+// 日志滚动设置
+func rolling(fLog *logrus.Logger, logFile string) {
+	// 设置输出
+	fLog.SetOutput(&lumberjack.Logger{
+		Filename:   logFile, //日志文件位置
+		MaxSize:    500,     // 单文件最大容量,单位是MB
+		MaxBackups: 3,       // 最大保留过期文件个数
+		MaxAge:     7,       // 保留过期文件的最大时间间隔,单位是天
+		Compress:   true,    // 是否需要压缩滚动日志, 使用的 gzip 压缩
+	})
 }