Browse Source

修改日志

xyxie 1 year ago
parent
commit
fd047b1c0f
6 changed files with 299 additions and 44 deletions
  1. 62 14
      controllers/base_auth.go
  2. 83 8
      controllers/base_common.go
  3. 2 1
      go.mod
  4. 2 0
      go.sum
  5. 23 0
      utils/config.go
  6. 127 21
      utils/logs.go

+ 62 - 14
controllers/base_auth.go

@@ -7,6 +7,7 @@ import (
 	"eta/eta_mobile/services/alarm_msg"
 	"eta/eta_mobile/services/data"
 	"fmt"
+	"github.com/sirupsen/logrus"
 	"net/http"
 	"net/url"
 	"strconv"
@@ -17,12 +18,8 @@ import (
 
 	"eta/eta_mobile/models"
 	"eta/eta_mobile/utils"
-
-	"github.com/rdlucklib/rdluck_tools/log"
 )
 
-var apiLog *log.Log
-
 type AfterHandle func(bodyByte []byte) error
 
 // AfterHandlerUrlMap 结束后待处理的url
@@ -40,15 +37,6 @@ var AfterHandlerUrlMap = map[string][]AfterHandle{
 	"/adminapi/datamanage/chart_info/copy":       {data.DeleteChartClassifyRedis},                                //图表另存为接口
 }
 
-func init() {
-	if utils.RunMode == "release" {
-		logDir := `/data/rdlucklog/eta_mobile`
-		apiLog = log.Init("20060102.api", logDir)
-	} else {
-		apiLog = log.Init("20060102.api")
-	}
-}
-
 type BaseAuthController struct {
 	web.Controller
 	SysUser *system.Admin
@@ -259,7 +247,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)
+	c.logUri(data, requestBody, ip)
 	if coding {
 		content = []byte(utils.StringsToJSON(string(content)))
 	}
@@ -326,3 +314,63 @@ func GetSysUserRoleTypeCode(roleTypeCode string) string {
 	}
 	return ""
 }
+
+func (c *BaseAuthController) logUri(data interface{}, requestBody, ip string) {
+	authorization := ""
+	method := c.Ctx.Input.Method()
+	uri := c.Ctx.Input.URI()
+	fmt.Println("Url:", uri)
+	if method != "HEAD" {
+		if method == "POST" || method == "GET" {
+			authorization = c.Ctx.Input.Header("authorization")
+			if authorization == "" {
+				authorization = c.Ctx.Input.Header("Authorization")
+			}
+			if authorization == "" {
+				newAuthorization := c.GetString("authorization")
+				if newAuthorization != `` {
+					authorization = "authorization=" + newAuthorization
+				} else {
+					newAuthorization = c.GetString("Authorization")
+					authorization = "authorization=" + newAuthorization
+				}
+			} else {
+				if strings.Contains(authorization, ";") {
+					authorization = strings.Replace(authorization, ";", "$", 1)
+				}
+			}
+			if authorization == "" {
+				strArr := strings.Split(uri, "?")
+				for k, v := range strArr {
+					fmt.Println(k, v)
+				}
+				if len(strArr) > 1 {
+					authorization = strArr[1]
+					authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
+					fmt.Println(authorization)
+				}
+			}
+		}
+	}
+
+	var reqData interface{}
+	err := json.Unmarshal([]byte(requestBody), &reqData)
+	if err != nil {
+		utils.ApiLog.WithFields(logrus.Fields{
+			"uri":           c.Ctx.Input.URI(),
+			"authorization": authorization,
+			"requestBody":   requestBody,
+			"responseBody":  data,
+			"ip":            ip,
+		}).Info("请求详情")
+	} else {
+		utils.ApiLog.WithFields(logrus.Fields{
+			"uri":           c.Ctx.Input.URI(),
+			"authorization": authorization,
+			"requestBody":   reqData,
+			"responseBody":  data,
+			"ip":            ip,
+		}).Info("请求详情")
+	}
+	return
+}

+ 83 - 8
controllers/base_common.go

@@ -6,24 +6,40 @@ import (
 	"eta/eta_mobile/utils"
 	"fmt"
 	"github.com/beego/beego/v2/server/web"
+	"github.com/sirupsen/logrus"
 	"net/http"
 	"net/url"
+	"strings"
 )
 
 type BaseCommonController struct {
 	web.Controller
 }
 
-func (this *BaseCommonController) Prepare() {
+func (c *BaseCommonController) Prepare() {
 	var requestBody string
-	method := this.Ctx.Input.Method()
+	method := c.Ctx.Input.Method()
 	if method == "GET" {
-		requestBody = this.Ctx.Request.RequestURI
+		requestBody = c.Ctx.Request.RequestURI
 	} else {
-		requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
+		requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
+	}
+	ip := c.Ctx.Input.IP()
+	var reqData interface{}
+	err := json.Unmarshal([]byte(requestBody), &reqData)
+	if err != nil {
+		utils.ApiLog.WithFields(logrus.Fields{
+			"uri":         c.Ctx.Input.URI(),
+			"requestBody": requestBody,
+			"ip":          ip,
+		}).Info("Prepare 请求详情")
+	} else {
+		utils.ApiLog.WithFields(logrus.Fields{
+			"uri":         c.Ctx.Input.URI(),
+			"requestBody": reqData,
+			"ip":          ip,
+		}).Info("Prepare 请求详情")
 	}
-	ip := this.Ctx.Input.IP()
-	apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
 }
 
 func (c *BaseCommonController) ServeJSON(encoding ...bool) {
@@ -67,8 +83,7 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
 	fmt.Println("params")
 	fmt.Println(params)
 	requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
-	apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
-
+	c.logUri(data, requestBody, ip)
 	if coding {
 		content = []byte(utils.StringsToJSON(string(content)))
 	}
@@ -85,3 +100,63 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
 
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseCommonController) logUri(data interface{}, requestBody, ip string) {
+	authorization := ""
+	method := c.Ctx.Input.Method()
+	uri := c.Ctx.Input.URI()
+	fmt.Println("Url:", uri)
+	if method != "HEAD" {
+		if method == "POST" || method == "GET" {
+			authorization = c.Ctx.Input.Header("authorization")
+			if authorization == "" {
+				authorization = c.Ctx.Input.Header("Authorization")
+			}
+			if authorization == "" {
+				newAuthorization := c.GetString("authorization")
+				if newAuthorization != `` {
+					authorization = "authorization=" + newAuthorization
+				} else {
+					newAuthorization = c.GetString("Authorization")
+					authorization = "authorization=" + newAuthorization
+				}
+			} else {
+				if strings.Contains(authorization, ";") {
+					authorization = strings.Replace(authorization, ";", "$", 1)
+				}
+			}
+			if authorization == "" {
+				strArr := strings.Split(uri, "?")
+				for k, v := range strArr {
+					fmt.Println(k, v)
+				}
+				if len(strArr) > 1 {
+					authorization = strArr[1]
+					authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
+					fmt.Println(authorization)
+				}
+			}
+		}
+	}
+
+	var reqData interface{}
+	err := json.Unmarshal([]byte(requestBody), &reqData)
+	if err != nil {
+		utils.ApiLog.WithFields(logrus.Fields{
+			"uri":           c.Ctx.Input.URI(),
+			"authorization": authorization,
+			"requestBody":   requestBody,
+			"responseBody":  data,
+			"ip":            ip,
+		}).Info("请求详情")
+	} else {
+		utils.ApiLog.WithFields(logrus.Fields{
+			"uri":           c.Ctx.Input.URI(),
+			"authorization": authorization,
+			"requestBody":   reqData,
+			"responseBody":  data,
+			"ip":            ip,
+		}).Info("请求详情")
+	}
+	return
+}

+ 2 - 1
go.mod

@@ -22,12 +22,14 @@ require (
 	github.com/rdlucklib/rdluck_tools v1.0.3
 	github.com/shopspring/decimal v1.3.1
 	github.com/silenceper/wechat/v2 v2.1.4
+	github.com/sirupsen/logrus v1.9.0
 	github.com/tealeg/xlsx v1.0.5
 	github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.655
 	github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses v1.0.655
 	github.com/xuri/excelize/v2 v2.7.1
 	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 (
@@ -75,7 +77,6 @@ require (
 	github.com/richardlehane/mscfb v1.0.4 // indirect
 	github.com/richardlehane/msoleps v1.0.3 // indirect
 	github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
-	github.com/sirupsen/logrus v1.9.0 // indirect
 	github.com/spf13/cast v1.4.1 // indirect
 	github.com/tidwall/gjson v1.14.1 // indirect
 	github.com/tidwall/match v1.1.1 // indirect

+ 2 - 0
go.sum

@@ -827,6 +827,8 @@ gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
 gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 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/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 23 - 0
utils/config.go

@@ -146,6 +146,18 @@ var (
 	TencentEmailTemplateID       uint64 // 云邮件模板ID
 )
 
+// 日志配置
+var (
+	LogPath     string //调用过程中的日志存放地址
+	LogFile     string
+	LogDataPath string //调用过程中图表相关的日志存放地址
+	LogDataFile string
+	BinLogPath  string //数据库相关的日志存放地址
+	BinLogFile  string
+	ApiLogPath  string //接口请求地址和接口返回值日志存放地址
+	ApiLogFile  string
+)
+
 func init() {
 	tmpRunMode, err := web.AppConfig.String("run_mode")
 	if err != nil {
@@ -250,6 +262,17 @@ func init() {
 		// ppt 转图片服务地址
 		Ppt2ImageUrl = config["ppt2_image_url"]
 	}
+	//日志配置
+	{
+		LogPath = config["log_path"]
+		LogFile = config["log_file"]
+		LogDataPath = config["log_data_path"]
+		LogDataFile = config["log_data_file"]
+		BinLogPath = config["binlog_path"]
+		BinLogFile = config["binlog_file"]
+		ApiLogPath = config["apilog_path"]
+		ApiLogFile = config["apilog_file"]
+	}
 
 	// ES配置
 	{

+ 127 - 21
utils/logs.go

@@ -3,35 +3,54 @@ 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 FileLogData *logs.BeeLogger
-var FileLogCygx *logs.BeeLogger
+const (
+	DefaultLogPath    = "./rdlucklog"
+	DefaultBinlogPath = "./binlog"
+	DefaultBinlogFile = "binlog.log"
+)
+
+var FileLog = logrus.New()
+var ApiLog = logrus.New()
+var FileLogData = logrus.New()
 var Binlog *logs.BeeLogger
 
 func init() {
-	FileLog = logs.NewLogger(1000000)
-	FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/eta_mobile.log"}`)
-	FileLog.EnableFuncCallDepth(true)
+	logPath := LogPath
+	if logPath == "" {
+		logPath = DefaultLogPath
+	}
+	logFile := LogFile
+	if logFile == "" {
+		logFile = "eta_mobile.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	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)
 
 	//初始化binlog日志
+	//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)
+	initApiLog()
+	initFileLogData()
 }
 
 type logConfig struct {
@@ -46,13 +65,100 @@ 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 initApiLog() {
+	logPath := ApiLogPath
+	if logPath == "" {
+		if RunMode == "release" {
+			logPath = `/data/etalogs/eta_mobile`
+		} else {
+			logPath = `./rdlucklog/api`
+		}
+	}
+	logFile := ApiLogFile
+	if logFile == "" {
+		logFile = "eta_mobile_api.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	// 使用滚动压缩方式记录日志
+	rolling(ApiLog, logFileName)
+	//rolling(bLogFileName)
+	// 设置日志输出JSON格式
+	jsonFormat := new(logrus.JSONFormatter)
+	jsonFormat.DisableHTMLEscape = true
+	jsonFormat.TimestampFormat = HlbFormatDateTime
+	ApiLog.SetFormatter(jsonFormat)
+}
+
+func initFileLogData() {
+	logPath := LogDataPath
+	if logPath == "" {
+		logPath = DefaultLogPath
+	}
+	logFile := LogDataFile
+	if logFile == "" {
+		logFile = "eta_mobile_data.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	// 使用滚动压缩方式记录日志
+	rolling(FileLogData, logFileName)
+	//rolling(bLogFileName)
+	// 设置日志输出JSON格式
+	jsonFormat := new(logrus.JSONFormatter)
+	jsonFormat.DisableHTMLEscape = true
+	jsonFormat.TimestampFormat = HlbFormatDateTime
+	FileLogData.SetFormatter(jsonFormat)
+}
+
+// 日志滚动设置
+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:     "",