Ver Fonte

优化日志

xyxie há 1 ano atrás
pai
commit
f2998cca6a
3 ficheiros alterados com 41 adições e 12 exclusões
  1. 25 1
      controllers/base_auth.go
  2. 10 9
      utils/constants.go
  3. 6 2
      utils/logs.go

+ 25 - 1
controllers/base_auth.go

@@ -3,6 +3,7 @@ package controllers
 import (
 	"encoding/json"
 	"fmt"
+	"github.com/sirupsen/logrus"
 	"net/http"
 	"net/url"
 
@@ -128,7 +129,30 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
 	if requestBody == "" {
 		requestBody = c.Ctx.Input.URI()
 	}
-	utils.ApiLog.Info("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
+	authorization := c.Ctx.Input.Header("authorization")
+	if authorization == "" {
+		authorization = c.Ctx.Input.Header("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("请求详情")
+	}
+
 	if coding {
 		content = []byte(utils.StringsToJSON(string(content)))
 	}

+ 10 - 9
utils/constants.go

@@ -4,15 +4,16 @@ import "time"
 
 // 常量定义
 const (
-	FormatTime                 = "15:04:05"            //时间格式
-	FormatDate                 = "2006-01-02"          //日期格式
-	FormatDateUnSpace          = "20060102"            //日期格式
-	FormatDateTime             = "2006-01-02 15:04:05" //完整时间格式
-	FormatDateTimeUnSpace      = "20060102150405"      //完整时间格式
-	FormatShortDateTimeUnSpace = "060102150405"        //省去开头两位年份的时间格式
-	FormatYearMonthDate        = "2006-01"             //日期格式
-	FormatYearMonthUnSpace     = "200601"              //年月的日期格式
-	PageSize15                 = 15                    //列表页每页数据量
+	FormatTime                 = "15:04:05"                //时间格式
+	FormatDate                 = "2006-01-02"              //日期格式
+	FormatDateUnSpace          = "20060102"                //日期格式
+	FormatDateTime             = "2006-01-02 15:04:05"     //完整时间格式
+	HlbFormatDateTime          = "2006-01-02_15:04:05.999" //完整时间格式
+	FormatDateTimeUnSpace      = "20060102150405"          //完整时间格式
+	FormatShortDateTimeUnSpace = "060102150405"            //省去开头两位年份的时间格式
+	FormatYearMonthDate        = "2006-01"                 //日期格式
+	FormatYearMonthUnSpace     = "200601"                  //年月的日期格式
+	PageSize15                 = 15                        //列表页每页数据量
 	PageSize5                  = 5
 	PageSize10                 = 10
 	PageSize20                 = 20

+ 6 - 2
utils/logs.go

@@ -36,6 +36,7 @@ func init() {
 	// 设置日志输出JSON格式
 	jsonFormat := new(logrus.JSONFormatter)
 	jsonFormat.DisableHTMLEscape = true
+	jsonFormat.TimestampFormat = HlbFormatDateTime
 	FileLog.SetFormatter(jsonFormat)
 	FileLog.SetReportCaller(true)
 	//LogInstance.SetFormatter(&logrus.TextFormatter{})
@@ -65,13 +66,14 @@ func initBinlog() {
 	// 设置日志输出JSON格式
 	jsonFormat := new(logrus.JSONFormatter)
 	jsonFormat.DisableHTMLEscape = true
+	jsonFormat.TimestampFormat = HlbFormatDateTime
 	Binlog.SetFormatter(jsonFormat)
 }
 func initApiLog() {
 	logPath := ApiLogPath
 	if logPath == "" {
 		if RunMode == "release" {
-			logPath = `/data/rdlucklog/` + APP_NAME_EN
+			logPath = `/data/etalogs/` + APP_NAME_EN
 		} else {
 			logPath = `./rdlucklog/api`
 		}
@@ -90,6 +92,7 @@ func initApiLog() {
 	// 设置日志输出JSON格式
 	jsonFormat := new(logrus.JSONFormatter)
 	jsonFormat.DisableHTMLEscape = true
+	jsonFormat.TimestampFormat = HlbFormatDateTime
 	ApiLog.SetFormatter(jsonFormat)
 }
 
@@ -98,9 +101,10 @@ func rolling(fLog *logrus.Logger, logFile string) {
 	// 设置输出
 	fLog.SetOutput(&lumberjack.Logger{
 		Filename:   logFile, //日志文件位置
-		MaxSize:    500,     // 单文件最大容量,单位是MB
+		MaxSize:    100,     // 单文件最大容量,单位是MB
 		MaxBackups: 3,       // 最大保留过期文件个数
 		MaxAge:     7,       // 保留过期文件的最大时间间隔,单位是天
 		Compress:   true,    // 是否需要压缩滚动日志, 使用的 gzip 压缩
+		LocalTime:  true,
 	})
 }