zhangchuanxing před 1 týdnem
rodič
revize
52d94fa4cf
7 změnil soubory, kde provedl 329 přidání a 71 odebrání
  1. 1 1
      .gitignore
  2. 55 13
      controllers/base_auth.go
  3. 53 11
      controllers/base_auth_mobile.go
  4. 49 38
      controllers/base_common.go
  5. 1 1
      models/db.go
  6. 28 0
      utils/config.go
  7. 142 7
      utils/logs.go

+ 1 - 1
.gitignore

@@ -10,4 +10,4 @@
 /static
 /README.md
 /utils/cert/
-
+cygxlogs/

+ 55 - 13
controllers/base_auth.go

@@ -9,22 +9,21 @@ import (
 	"net/http"
 	"net/url"
 	"strconv"
-
-	"github.com/rdlucklib/rdluck_tools/log"
+	"strings"
 
 	"github.com/beego/beego/v2/server/web"
 )
 
-var apiLog *log.Log
-
-func init() {
-	if utils.RunMode == "release" {
-		logDir := `/data/rdlucklog/hongze_mfyx`
-		apiLog = log.Init("20060102.api", logDir)
-	} else {
-		apiLog = log.Init("20060102.api")
-	}
-}
+//var apiLog *log.Log
+//
+//func init() {
+//	if utils.RunMode == "release" {
+//		logDir := `/data/rdlucklog/hongze_mfyx`
+//		apiLog = log.Init("20060102.api", logDir)
+//	} else {
+//		apiLog = log.Init("20060102.api")
+//	}
+//}
 
 type BaseAuthController struct {
 	web.Controller
@@ -136,9 +135,52 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
 	}
 	ip := c.Ctx.Input.IP()
 	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(content, requestBody, ip)
+	//apiLog.Println("请求地址:", 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)))
 	}
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseAuthController) logUri(respContent []byte, 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)
+				}
+			}
+		}
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 53 - 11
controllers/base_auth_mobile.go

@@ -8,21 +8,20 @@ import (
 	"net/http"
 	"net/url"
 	"strconv"
+	"strings"
 
 	"hongze/hongze_mfyx/models"
 	"hongze/hongze_mfyx/utils"
-
-	"github.com/rdlucklib/rdluck_tools/log"
 )
 
-func init() {
-	if utils.RunMode == "release" {
-		logDir := `/data/rdlucklog/hongze_mfyx`
-		apiLog = log.Init("20060102.api", logDir)
-	} else {
-		apiLog = log.Init("20060102.api")
-	}
-}
+//func init() {
+//	if utils.RunMode == "release" {
+//		logDir := `/data/rdlucklog/hongze_mfyx`
+//		apiLog = log.Init("20060102.api", logDir)
+//	} else {
+//		apiLog = log.Init("20060102.api")
+//	}
+//}
 
 type BaseAuthMobileController struct {
 	web.Controller
@@ -135,9 +134,52 @@ func (c *BaseAuthMobileController) JSON(data interface{}, hasIndent bool, coding
 	}
 	ip := c.Ctx.Input.IP()
 	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(content, requestBody, ip)
+	//apiLog.Println("请求地址:", 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)))
 	}
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseAuthMobileController) logUri(respContent []byte, 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)
+				}
+			}
+		}
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 49 - 38
controllers/base_common.go

@@ -2,6 +2,7 @@ package controllers
 
 import (
 	"encoding/json"
+	"fmt"
 	"net/http"
 	"net/url"
 	"strings"
@@ -26,8 +27,8 @@ func (this *BaseCommonController) Prepare() {
 	} else {
 		requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
 	}
-	ip := this.Ctx.Input.IP()
-	apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
+	//ip := this.Ctx.Input.IP()
+	//apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
 
 	authorization := this.Ctx.Input.Header("Authorization")
 	if authorization == "" {
@@ -40,41 +41,8 @@ func (this *BaseCommonController) Prepare() {
 	if strings.Contains(uri, "/api/wechat/login") {
 		authorization = ""
 	}
-	if authorization != "" {
-		//session, err := models.GetSessionByToken(authorization)
-		//if err != nil {
-		//	if err.Error() == utils.ErrNoRow() {
-		//		this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
-		//		this.StopRun()
-		//		return
-		//	}
-		//	this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
-		//	this.StopRun()
-		//	return
-		//}
-		//if session == nil {
-		//	this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
-		//	this.StopRun()
-		//	return
-		//}
-		//wxUser, err := models.GetWxUserItemByUserId(session.UserId)
-		//if err != nil {
-		//	if err.Error() == utils.ErrNoRow() {
-		//		this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.UserId)}, false, false)
-		//		this.StopRun()
-		//		return
-		//	}
-		//	this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取wx_user信息异常,Eerr:" + err.Error()}, false, false)
-		//	this.StopRun()
-		//	return
-		//}
-		//if wxUser == nil {
-		//	this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
-		//	this.StopRun()
-		//	return
-		//}
-		//this.User = wxUser
-	}
+	ip := this.Ctx.Input.IP()
+	utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", this.Ctx.Input.URI(), requestBody, ip)
 	//this.Token = authorization
 }
 
@@ -115,10 +83,53 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
 	}
 	ip := c.Ctx.Input.IP()
 	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(content, requestBody, ip)
+	//apiLog.Println("请求地址:", 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)))
 	}
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseCommonController) logUri(respContent []byte, 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)
+				}
+			}
+		}
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 1 - 1
models/db.go

@@ -190,7 +190,7 @@ func init() {
 
 	// 记录ORM查询日志
 	orm.Debug = true
-	orm.DebugLog = orm.NewLog(utils.BinLog)
+	orm.DebugLog = orm.NewLog(utils.Binlog)
 
 }
 

+ 28 - 0
utils/config.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"github.com/beego/beego/v2/server/web"
 	"github.com/rdlucklib/rdluck_tools/cache"
+	"strconv"
 )
 
 var (
@@ -110,6 +111,19 @@ var (
 	SendWxCategoryTemplateMsgUrl string
 )
 
+// 日志配置
+var (
+	LogPath     string //调用过程中的日志存放地址
+	LogFile     string
+	LogDataPath string //调用过程中图表相关的日志存放地址
+	LogDataFile string
+	BinLogPath  string //数据库相关的日志存放地址
+	BinLogFile  string
+	ApiLogPath  string //接口请求地址和接口返回值日志存放地址
+	ApiLogFile  string
+	LogMaxDays  int //日志最大保留天数
+)
+
 func init() {
 	tmpRunMode, err := web.AppConfig.String("run_mode")
 	if err != nil {
@@ -300,6 +314,20 @@ func init() {
 		RAI_MOBILE_GE_LIN = "18258485832"     // 权益葛琳手机号
 	}
 
+	//日志配置
+	{
+		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"]
+		logMaxDaysStr := config["log_max_day"]
+		LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
+	}
+
 	//易董开放api配置
 	YiDongApiConfig()
 }

+ 142 - 7
utils/logs.go

@@ -1,20 +1,155 @@
 package utils
 
 import (
+	"encoding/json"
 	"github.com/beego/beego/v2/core/logs"
 	"os"
-	"time"
+	"path"
+)
+
+//var FileLog *logs.BeeLogger
+//var BinLog *logs.BeeLogger
+//
+//func init() {
+//	FileLog = logs.NewLogger(1000000)
+//	FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/hongze_mfyx.log"}`)
+//
+//	binLogDir := `./binlog`
+//	os.MkdirAll(binLogDir, os.ModePerm)
+//	BinLog = logs.NewLogger(1000000)
+//	BinLog.SetLogger(logs.AdapterMultiFile, `{"filename":"./binlog/binlog.`+time.Now().Format(FormatDate)+`.log","maxdays":30}`)
+//}
+
+const (
+	DefaultLogPath    = "./cygxlogs/filelog"
+	DefaultBinlogPath = "./cygxlogs/binlog"
+	DefaultDataPath   = "./cygxlogs/datalog"
+	DefaultApiLogPath = "./cygxlogs/apilog"
 )
 
 var FileLog *logs.BeeLogger
-var BinLog *logs.BeeLogger
+var ApiLog *logs.BeeLogger
+var FileLogData *logs.BeeLogger
+var Binlog *logs.BeeLogger
 
 func init() {
+	if LogMaxDays == 0 {
+		LogMaxDays = 7
+	}
+	logPath := LogPath
+	if logPath == "" {
+		logPath = DefaultLogPath
+	}
+	logFile := LogFile
+	if logFile == "" {
+		logFile = "filelog.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
 	FileLog = logs.NewLogger(1000000)
-	FileLog.SetLogger(logs.AdapterFile, `{"filename":"./rdlucklog/hongze_mfyx.log"}`)
+	logConf := getDefaultLogConfig()
+
+	logConf.FileName = logFileName
+	b, _ := json.Marshal(logConf)
+	FileLog.SetLogger(logs.AdapterFile, string(b))
+	FileLog.EnableFuncCallDepth(true)
+
+	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 == "" {
+		binlogPath = DefaultBinlogPath
+	}
+	binlogFile := BinLogFile
+	if binlogFile == "" {
+		binlogFile = "binlog.log"
+	}
+	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 == "" {
+		logPath = DefaultApiLogPath
+	}
+	logFile := ApiLogFile
+	if logFile == "" {
+		logFile = "apilog.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	ApiLog = logs.NewLogger(1000000)
+	logConf := getDefaultLogConfig()
+
+	logConf.FileName = logFileName
+	b, _ := json.Marshal(logConf)
+	ApiLog.SetLogger(logs.AdapterFile, string(b))
+	ApiLog.EnableFuncCallDepth(true)
+}
+
+func initFileLogData() {
+	logPath := LogDataPath
+	if logPath == "" {
+		logPath = DefaultDataPath
+	}
+	logFile := LogDataFile
+	if logFile == "" {
+		logFile = "datalog.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	FileLogData = logs.NewLogger(1000000)
+	logConf := getDefaultLogConfig()
+
+	logConf.FileName = logFileName
+	b, _ := json.Marshal(logConf)
+	FileLogData.SetLogger(logs.AdapterFile, string(b))
+	FileLogData.EnableFuncCallDepth(true)
+}
 
-	binLogDir := `./binlog`
-	os.MkdirAll(binLogDir, os.ModePerm)
-	BinLog = logs.NewLogger(1000000)
-	BinLog.SetLogger(logs.AdapterMultiFile, `{"filename":"./binlog/binlog.`+time.Now().Format(FormatDate)+`.log","maxdays":30}`)
+func getDefaultLogConfig() logConfig {
+	return logConfig{
+		FileName: "",
+		MaxLines: 10000000,
+		MaxSize:  1 << 28,
+		Daily:    true,
+		MaxDays:  LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
+		Rotate:   true,
+		Level:    logs.LevelTrace,
+		//Perm:     "",
+	}
 }