فهرست منبع

Merge remote-tracking branch 'origin/master'

ziwen 1 سال پیش
والد
کامیت
664e863df8
6فایلهای تغییر یافته به همراه119 افزوده شده و 48 حذف شده
  1. 5 9
      controllers/base_auth.go
  2. 3 0
      go.mod
  3. 4 0
      go.sum
  4. 1 1
      models/db.go
  5. 19 0
      utils/config.go
  6. 87 38
      utils/logs.go

+ 5 - 9
controllers/base_auth.go

@@ -10,12 +10,8 @@ import (
 
 	"eta/eta_index_lib/models"
 	"eta/eta_index_lib/utils"
-
-	"github.com/rdlucklib/rdluck_tools/log"
 )
 
-var apiLog *log.Log
-
 type AfterHandle func(bodyByte []byte) error
 
 // AfterHandlerUrlMap 结束后待处理的url
@@ -24,12 +20,12 @@ var AfterHandlerUrlMap = map[string][]AfterHandle{
 }
 
 func init() {
-	if utils.RunMode == "release" {
+	/*if utils.RunMode == "release" {
 		logDir := `/data/rdlucklog/` + utils.APP_NAME_EN
-		apiLog = log.Init("20060102.api", logDir)
+		ApiLog = log.Init("20060102.api", logDir)
 	} else {
-		apiLog = log.Init("20060102.api")
-	}
+		ApiLog = log.Init("20060102.api")
+	}*/
 }
 
 type BaseAuthController struct {
@@ -132,7 +128,7 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
 	if requestBody == "" {
 		requestBody = c.Ctx.Input.URI()
 	}
-	apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
+	utils.ApiLog.Info("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
 	if coding {
 		content = []byte(utils.StringsToJSON(string(content)))
 	}

+ 3 - 0
go.mod

@@ -13,8 +13,10 @@ 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
 	gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
+	gopkg.in/natefinch/lumberjack.v2 v2.2.1
 )
 
 require (
@@ -31,6 +33,7 @@ require (
 	github.com/gonum/matrix v0.0.0-20181209220409-c518dec07be9 // 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

@@ -278,6 +278,7 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
 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=
@@ -435,6 +436,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/smartwalle/pongo2render v1.0.1/go.mod h1:MGnTzND7nEMz7g194kjlnw8lx/V5JJlb1hr5kDXEO0I=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
@@ -731,6 +733,8 @@ gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkp
 gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
 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

@@ -32,7 +32,7 @@ func init() {
 	gl.SetConnMaxLifetime(10 * time.Minute)
 
 	orm.Debug = true
-	orm.DebugLog = orm.NewLog(utils.Binlog)
+	orm.DebugLog = orm.NewLog(utils.Binlog.Writer())
 
 	//注册对象
 	orm.RegisterModel(

+ 19 - 0
utils/config.go

@@ -82,6 +82,15 @@ var (
 	InitAdminName string
 )
 
+var (
+	LogPath    string
+	LogFile    string
+	BinLogPath string
+	BinLogFile string
+	ApiLogPath string
+	ApiLogFile string
+)
+
 func init() {
 	tmpRunMode, err := web.AppConfig.String("run_mode")
 	if err != nil {
@@ -189,6 +198,16 @@ func init() {
 		DATA_INDEX_NAME = config["data_index_name"]
 	}
 
+	//日志配置
+	{
+		LogPath = config["log_path"]
+		LogFile = config["log_file"]
+		BinLogPath = config["binlog_path"]
+		BinLogFile = config["binlog_file"]
+		ApiLogPath = config["apilog_path"]
+		ApiLogFile = config["apilog_file"]
+	}
+
 	// 数据指标库ES索引名称
 	InitAdminId = config["init_admin_id"]
 

+ 87 - 38
utils/logs.go

@@ -1,57 +1,106 @@
 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
-var Binlog *logs.BeeLogger
+const (
+	DefaultLogPath    = "./rdlucklog"
+	DefaultBinlogPath = "./binlog"
+	DefaultBinlogFile = "binlog.log"
+)
+
+var FileLog = logrus.New()
+var Binlog = logrus.New()
+var ApiLog = logrus.New()
 
 func init() {
-	FileLog = logs.NewLogger(1000000)
-	FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/eta_index_lib.log"}`)
-	FileLog.EnableFuncCallDepth(true)
+	logPath := LogPath
+	if logPath == "" {
+		logPath = DefaultLogPath
+	}
+	logFile := LogFile
+	if logFile == "" {
+		logFile = APP_NAME_EN + ".log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	// 使用滚动压缩方式记录日志
+	rolling(FileLog, logFileName)
+	//rolling(bLogFileName)
+	// 设置日志输出JSON格式
+	jsonFormat := new(logrus.JSONFormatter)
+	jsonFormat.DisableHTMLEscape = true
+	FileLog.SetFormatter(jsonFormat)
+	FileLog.SetReportCaller(true)
+	//LogInstance.SetFormatter(&logrus.TextFormatter{})
+	// 设置日志记录级别
+	//FileLog.SetLevel(logrus.DebugLevel)
 
+	//FileLog.Info("abc")
 	initBinlog()
+	initApiLog()
 }
 
 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)
+	binlogPath := BinLogPath
+	if binlogPath == "" {
+		binlogPath = DefaultBinlogPath
+	}
+	binlogFile := BinLogFile
+	if binlogFile == "" {
+		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
+	Binlog.SetFormatter(jsonFormat)
 }
+func initApiLog() {
+	logPath := ApiLogPath
+	if logPath == "" {
+		if RunMode == "release" {
+			logPath = `/data/rdlucklog/` + APP_NAME_EN
+		} else {
+			logPath = `./rdlucklog/api`
+		}
+	}
+	logFile := ApiLogFile
+	if logFile == "" {
+		logFile = APP_NAME_EN + "_api.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
 
-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:"日志文件权限"`
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	// 使用滚动压缩方式记录日志
+	rolling(ApiLog, logFileName)
+	//rolling(bLogFileName)
+	// 设置日志输出JSON格式
+	jsonFormat := new(logrus.JSONFormatter)
+	jsonFormat.DisableHTMLEscape = true
+	ApiLog.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 压缩
+	})
 }