Compare commits

..

No commits in common. "v1.2.0" and "master" have entirely different histories.

2 changed files with 26 additions and 37 deletions

7
api.go
View File

@ -117,12 +117,13 @@ func middlewareMyApiEngine() gin.HandlerFunc {
defer log.Cleanup() defer log.Cleanup()
// Step 2. init *api.Context // Step 2. init *api.Context
req := c.Request
log.DebugfWithDepth(1, "[%s|%s|%s]", req.Method, req.Host, req.RequestURI)
cc := New(c) cc := New(c)
cc.Set("cc", cc) c.Set("cc", cc)
defer cc.addApiCallNoticeLog()()
// Step 3. do request // Step 3. do request
cc.Next() c.Next()
// Step 4. recorde metrics // Step 4. recorde metrics
cc.recordMetrics() cc.recordMetrics()

View File

@ -27,8 +27,7 @@ type Context struct {
resultentry string resultentry string
httpstatus int httpstatus int
starttime time.Time starttime time.Time
output interface{} // case 1: for user get real errcode; case 2: for notice log output interface{}
noticedepth int
errcode int errcode int
} }
@ -131,7 +130,10 @@ func (c *Context) RESULT(output interface{}) error {
} }
var t = reflect.TypeOf(output) var t = reflect.TypeOf(output)
defer func(o *interface{}) { defer func(o *interface{}) {
b, err := json.Marshal(o)
if err != nil {
return
}
calldepth := 0 calldepth := 0
switch c.resultentry { switch c.resultentry {
case "RESULT": case "RESULT":
@ -141,8 +143,19 @@ func (c *Context) RESULT(output interface{}) error {
case "RESULT_PARAMETER_ERROR": case "RESULT_PARAMETER_ERROR":
calldepth = 2 calldepth = 2
} }
c.output = o //// cut log if too long ////// begin //////////////
c.noticedepth = calldepth if len(b) > 1024 {
//no := retag.Convert(*o, retag.NewView("json", "!logignore"))
//if nb, err := json.Marshal(no); err != nil {
// return
//} else {
// b = nb
//}
}
//// cut log if too long ////// end ////////////////
cost := time.Now().Sub(c.starttime).Milliseconds()
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
log.NoticefWithDepth(calldepth, noticeStr)
}(&output) }(&output)
if _, ok := t.FieldByName("ErrCode"); !ok { if _, ok := t.FieldByName("ErrCode"); !ok {
@ -190,14 +203,6 @@ func (c *Context) ApiHandler() Handler {
return ApiHandler(c.Context) return ApiHandler(c.Context)
} }
func (c *Context) ApiHandlerWithNoNil() Handler {
h := c.ApiHandler()
if h == nil {
h = unimplementHandler{c.Context}
}
return h
}
func (c *Context) shapeOutput(o interface{}) { func (c *Context) shapeOutput(o interface{}) {
if c.output == nil { if c.output == nil {
return return
@ -214,32 +219,15 @@ func (c *Context) shapeOutput(o interface{}) {
return return
} }
func (c *Context) addApiCallNoticeLog() func() {
h, req := c.ApiHandlerWithNoNil(), c.Request
log.DebugfWithDepth(1, "[>>>>>>:%s,%s,%s]", h.ApiName(), req.Method, req.RequestURI)
return func() {
var (
o = c.output
b, err = json.Marshal(o)
costus = time.Now().Sub(c.starttime).Microseconds()
costms = float64(costus) / 1000.0
)
if err != nil {
b = []byte(fmt.Sprintf("marshal output error:%s", err))
}
if len(b) > 1024 { // cut log if too long
b = append(b[0:1000], b[len(b)-24:]...)
}
log.Noticef("[<<<<<<:%s,code:%d,cost:%dms] output: '%s", h.ApiName(), c.errcode, int64(costms), string(b))
}
}
func (c *Context) recordMetrics() { func (c *Context) recordMetrics() {
if metricApiCounter == nil || if metricApiCounter == nil ||
metricApiSummary == nil { metricApiSummary == nil {
return return
} }
var h = c.ApiHandlerWithNoNil() var h = c.ApiHandler()
if h == nil {
h = unimplementHandler{c.Context}
}
var ( var (
api = h.ApiName() api = h.ApiName()
appid = c.AppId appid = c.AppId