|
|
@@ -7,6 +7,7 @@ import (
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/gocql/gocql"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
@@ -89,7 +90,7 @@ type logInfo struct {
|
|
|
RespMsg string // 由 resp body 解析
|
|
|
RespData string // 由 resp body 解析
|
|
|
|
|
|
- CostTime int
|
|
|
+ CostTime float64
|
|
|
}
|
|
|
|
|
|
func LoggerCassandra()gin.HandlerFunc{
|
|
|
@@ -142,32 +143,38 @@ func insertLogInfo(logInfo *logInfo){
|
|
|
defer session.Close()
|
|
|
|
|
|
var respBodyObj struct {
|
|
|
- Code string
|
|
|
+ Code int
|
|
|
Msg string
|
|
|
- Data string
|
|
|
+ Data interface{}
|
|
|
}
|
|
|
if logInfo.RespBody != ""{
|
|
|
+ fmt.Println(logInfo.RespBody)
|
|
|
err = json.Unmarshal([]byte(logInfo.RespBody), &respBodyObj)
|
|
|
if err != nil{
|
|
|
fmt.Println("json.Unmarshal 错误", err.Error())
|
|
|
}else {
|
|
|
- logInfo.RespCode = respBodyObj.Code
|
|
|
+ logInfo.RespCode = strconv.Itoa(respBodyObj.Code)
|
|
|
logInfo.RespMsg = respBodyObj.Msg
|
|
|
- logInfo.RespData = respBodyObj.Data
|
|
|
+ if respBodyObj.Data != nil{
|
|
|
+ str, err := json.Marshal(respBodyObj.Data)
|
|
|
+ if err == nil{
|
|
|
+ logInfo.RespData = string(str)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- logInfo.CostTime = int(logInfo.RespTime.Sub(logInfo.ReqTime))
|
|
|
+ logInfo.CostTime = float64(logInfo.RespTime.Sub(logInfo.ReqTime).Seconds())
|
|
|
|
|
|
// 保存轨迹
|
|
|
- fmt.Println("保存轨迹")
|
|
|
cqlformat := `insert into
|
|
|
log_info(app_name, req_date, req_time, req_method, req_url, req_proto, req_ua, req_referer, req_post_data, resp_time, resp_body, resp_code, resp_msg, resp_data, cost_time)
|
|
|
values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);`
|
|
|
reqDate := logInfo.ReqTime.Format("20060102")
|
|
|
- q := session.Query(cqlformat, __appName, reqDate, logInfo.ReqTime.Format("2006-01-02 15:04:05"), logInfo.ReqMethod, logInfo.ReqUrl, logInfo.ReqProto, logInfo.ReqUa, logInfo.ReqReferer, logInfo.ReqPostData, logInfo.RespTime.Format("2006-01-02 15:04:05"), logInfo.RespBody, logInfo.RespCode, logInfo.RespMsg, logInfo.RespData, logInfo.CostTime)
|
|
|
- err = q.Exec() // 应该可以使用批量插入
|
|
|
- if err != nil{
|
|
|
- fmt.Println("插入日志错误:", err)
|
|
|
+ //q := session.Query(cqlformat, __appName, reqDate, logInfo.ReqTime.Format("2006-01-02 15:04:05"), logInfo.ReqMethod, logInfo.ReqUrl, logInfo.ReqProto, logInfo.ReqUa, logInfo.ReqReferer, logInfo.ReqPostData, logInfo.RespTime.Format("2006-01-02 15:04:05"), logInfo.RespBody, logInfo.RespCode, logInfo.RespMsg, logInfo.RespData, logInfo.CostTime)
|
|
|
+ batch := session.NewBatch(gocql.UnloggedBatch) // 应该批量插入多条,例如20条
|
|
|
+ batch.Query(cqlformat, __appName, reqDate, logInfo.ReqTime.Format("2006-01-02 15:04:05"), logInfo.ReqMethod, logInfo.ReqUrl, logInfo.ReqProto, logInfo.ReqUa, logInfo.ReqReferer, logInfo.ReqPostData, logInfo.RespTime.Format("2006-01-02 15:04:05"), logInfo.RespBody, logInfo.RespCode, logInfo.RespMsg, logInfo.RespData, logInfo.CostTime)
|
|
|
+ if err := session.ExecuteBatch(batch); err != nil {
|
|
|
+ fmt.Println("批量插入轨迹错误:", err)
|
|
|
return
|
|
|
}
|
|
|
}
|