优化开放接口日志分类
This commit is contained in:
@@ -11,11 +11,32 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
CtxReqID = "open_req_id"
|
||||
CtxDebug = "open_debug"
|
||||
CtxBody = "open_body"
|
||||
CtxStart = "open_start"
|
||||
CtxAPIKey = "open_api_key"
|
||||
CtxReqID = "open_req_id"
|
||||
CtxDebug = "open_debug"
|
||||
CtxBody = "open_body"
|
||||
CtxStart = "open_start"
|
||||
CtxAPIKey = "open_api_key"
|
||||
CtxSide = "open_side"
|
||||
CtxAPIType = "open_api_type"
|
||||
CtxAction = "open_action"
|
||||
)
|
||||
|
||||
const (
|
||||
SideClient = "client"
|
||||
SideSource = "source"
|
||||
|
||||
APITypeClientV1 = "client_v1"
|
||||
APITypeSourceOpenV1 = "source_open_v1"
|
||||
APITypeUnknown = "unknown"
|
||||
|
||||
ActionAuth = "auth"
|
||||
ActionList = "list"
|
||||
ActionCreate = "create"
|
||||
ActionQuery = "query"
|
||||
ActionCancel = "cancel"
|
||||
ActionWallet = "wallet"
|
||||
ActionPush = "push"
|
||||
ActionUnknown = "unknown"
|
||||
)
|
||||
|
||||
// Enabled 是否开启开放接口详细日志
|
||||
@@ -50,6 +71,38 @@ func EnsureReqID(c *gin.Context) string {
|
||||
return id
|
||||
}
|
||||
|
||||
func SetScope(c *gin.Context, side, action string) {
|
||||
c.Set(CtxSide, side)
|
||||
c.Set(CtxAPIType, APITypeFromSide(side))
|
||||
c.Set(CtxAction, action)
|
||||
}
|
||||
|
||||
func SetAction(c *gin.Context, action string) {
|
||||
c.Set(CtxAction, action)
|
||||
}
|
||||
|
||||
func ScopeFromPath(side, method, path string) (string, string) {
|
||||
action := ActionUnknown
|
||||
switch side {
|
||||
case SideClient:
|
||||
action = clientAction(method, path)
|
||||
case SideSource:
|
||||
action = sourceAction(method, path)
|
||||
}
|
||||
return side, action
|
||||
}
|
||||
|
||||
func APITypeFromSide(side string) string {
|
||||
switch side {
|
||||
case SideClient:
|
||||
return APITypeClientV1
|
||||
case SideSource:
|
||||
return APITypeSourceOpenV1
|
||||
default:
|
||||
return APITypeUnknown
|
||||
}
|
||||
}
|
||||
|
||||
func IsDebug(c *gin.Context) bool {
|
||||
if !Enabled {
|
||||
return false
|
||||
@@ -92,12 +145,37 @@ func Truncate(s string, max int) string {
|
||||
return s[:max] + fmt.Sprintf("...(%d bytes)", len(s))
|
||||
}
|
||||
|
||||
func Prefix(c *gin.Context) string {
|
||||
side, _ := c.Get(CtxSide)
|
||||
apiType, _ := c.Get(CtxAPIType)
|
||||
action, _ := c.Get(CtxAction)
|
||||
sideText, _ := side.(string)
|
||||
apiTypeText, _ := apiType.(string)
|
||||
actionText, _ := action.(string)
|
||||
tag := "open"
|
||||
if apiTypeText != "" && apiTypeText != APITypeUnknown {
|
||||
tag += "." + apiTypeText
|
||||
} else if sideText != "" {
|
||||
tag += "." + sideText
|
||||
}
|
||||
if actionText != "" {
|
||||
tag += "." + actionText
|
||||
}
|
||||
fields := ""
|
||||
if apiTypeText != "" {
|
||||
fields += fmt.Sprintf(" api_type=%s", apiTypeText)
|
||||
}
|
||||
if actionText != "" {
|
||||
fields += fmt.Sprintf(" action=%s", actionText)
|
||||
}
|
||||
return fmt.Sprintf("[%s] req_id=%s%s ", tag, GetReqID(c), fields)
|
||||
}
|
||||
|
||||
func Info(c *gin.Context, format string, args ...interface{}) {
|
||||
if !IsDebug(c) {
|
||||
return
|
||||
}
|
||||
prefix := fmt.Sprintf("[open] req_id=%s ", GetReqID(c))
|
||||
log.Printf(prefix+format, args...)
|
||||
log.Printf(Prefix(c)+format, args...)
|
||||
}
|
||||
|
||||
func Warn(c *gin.Context, format string, args ...interface{}) {
|
||||
@@ -105,8 +183,7 @@ func Warn(c *gin.Context, format string, args ...interface{}) {
|
||||
if !Enabled {
|
||||
return
|
||||
}
|
||||
prefix := fmt.Sprintf("[open] req_id=%s ", GetReqID(c))
|
||||
log.Printf(prefix+format, args...)
|
||||
log.Printf(Prefix(c)+format, args...)
|
||||
}
|
||||
|
||||
func Elapsed(c *gin.Context) time.Duration {
|
||||
@@ -117,3 +194,33 @@ func Elapsed(c *gin.Context) time.Duration {
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func clientAction(method, path string) string {
|
||||
method = strings.ToUpper(method)
|
||||
switch {
|
||||
case method == "GET" && path == "/api/client/v1/products":
|
||||
return ActionList
|
||||
case method == "POST" && path == "/api/client/v1/orders":
|
||||
return ActionCreate
|
||||
case method == "GET" && strings.HasPrefix(path, "/api/client/v1/orders/"):
|
||||
return ActionQuery
|
||||
case method == "POST" && strings.HasPrefix(path, "/api/client/v1/orders/") && strings.HasSuffix(path, "/cancel"):
|
||||
return ActionCancel
|
||||
case method == "GET" && path == "/api/client/v1/wallet":
|
||||
return ActionWallet
|
||||
default:
|
||||
return ActionUnknown
|
||||
}
|
||||
}
|
||||
|
||||
func sourceAction(method, path string) string {
|
||||
method = strings.ToUpper(method)
|
||||
switch {
|
||||
case method == "GET" && strings.HasPrefix(path, "/api/open/v1/orders/"):
|
||||
return ActionQuery
|
||||
case method == "POST" && path == "/api/open/v1/orders/ship-notify":
|
||||
return ActionPush
|
||||
default:
|
||||
return ActionUnknown
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user