Skip to content

Commit

Permalink
Update time 包
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Sep 4, 2019
1 parent fd2b24a commit 54c7c2a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
24 changes: 12 additions & 12 deletions app/route/middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ func SetUp() gin.HandlerFunc {
c.Writer = bodyLogWriter

// 开始时间
startTime := util.GetCurrentMilliTime()
startTime := util.GetCurrentMilliUnix()

// 处理请求
c.Next()

responseBody := bodyLogWriter.body.String()

var responseCode int
var responseMsg string
var responseMsg string
var responseData interface{}

if responseBody != "" {
response := util.Response{}
err := json.Unmarshal([]byte(responseBody), &response)
if err == nil {
responseCode = response.Code
responseMsg = response.Message
responseMsg = response.Message
responseData = response.Data
}
}

// 结束时间
endTime := util.GetCurrentMilliTime()
endTime := util.GetCurrentMilliUnix()

if c.Request.Method == "POST" {
_ = c.Request.ParseForm()
Expand All @@ -68,21 +68,21 @@ func SetUp() gin.HandlerFunc {
// 日志格式
accessLogMap := make(map[string]interface{})

accessLogMap["request_time"] = startTime
accessLogMap["request_method"] = c.Request.Method
accessLogMap["request_uri"] = c.Request.RequestURI
accessLogMap["request_proto"] = c.Request.Proto
accessLogMap["request_ua"] = c.Request.UserAgent()
accessLogMap["request_referer"] = c.Request.Referer()
accessLogMap["request_time"] = startTime
accessLogMap["request_method"] = c.Request.Method
accessLogMap["request_uri"] = c.Request.RequestURI
accessLogMap["request_proto"] = c.Request.Proto
accessLogMap["request_ua"] = c.Request.UserAgent()
accessLogMap["request_referer"] = c.Request.Referer()
accessLogMap["request_post_data"] = c.Request.PostForm.Encode()
accessLogMap["request_client_ip"] = c.ClientIP()

accessLogMap["response_time"] = endTime
accessLogMap["response_code"] = responseCode
accessLogMap["response_msg"] = responseMsg
accessLogMap["response_msg"] = responseMsg
accessLogMap["response_data"] = responseData

accessLogMap["cost_time"] = fmt.Sprintf("%vms", endTime - startTime)
accessLogMap["cost_time"] = fmt.Sprintf("%vms", endTime-startTime)

accessLogJson, _ := util.JsonEncode(accessLogMap)

Expand Down
18 changes: 14 additions & 4 deletions app/util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package util

import "time"

//获取当前的Unix时间戳
func GetCurrentTime() int64 {
// 获取当前的时间 - 字符串
func GetCurrentDate() string {
return time.Now().Format("2006/01/02 15:04:05")
}

// 获取当前的时间 - Unix时间戳
func GetCurrentUnix() int64 {
return time.Now().Unix()
}

//获取当前的毫秒级时间戳
func GetCurrentMilliTime() int64 {
// 获取当前的时间 - 毫秒级时间戳
func GetCurrentMilliUnix() int64 {
return time.Now().UnixNano() / 1000000
}

// 获取当前的时间 - 纳秒级时间戳
func GetCurrentNanoUnix() int64 {
return time.Now().UnixNano()
}

0 comments on commit 54c7c2a

Please sign in to comment.