-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgokitlog.go
52 lines (44 loc) · 1.17 KB
/
gokitlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gokitlog
import (
"time"
"github.com/gin-gonic/gin"
)
// ErrorLogger returns an ErrorLoggerT with parameter gin.ErrorTypeAny
func ErrorLogger() gin.HandlerFunc {
return ErrorLoggerT(gin.ErrorTypeAny)
}
// ErrorLoggerT returns an ErrorLoggerT middleware with the given
// type gin.ErrorType.
func ErrorLoggerT(typ gin.ErrorType) gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if !c.Writer.Written() {
json := c.Errors.ByType(typ).JSON()
if json != nil {
c.JSON(-1, json)
}
}
}
}
// LogAdapter returns a new LogAdapter wrapper around the passed
// logger. It formats the log entries similar to
// http://godoc.org/github.com/gin-gonic/gin#Logger does.
//
// Example:
// router := gin.New()
// router.Use(gokitlog.LogAdapter(func(c *gin.Context){
// latency,_ := c.Get("latency")
// logger.Log(
// "latency",latency,"clientIP",c.ClientIP(),
// "status",c.Writer.Status(),"err",c.Errors.String())
// })
func LogAdapter(fn func(c *gin.Context)) gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
// process request
c.Next()
latency := time.Since(t)
c.Set("latency", latency)
fn(c)
}
}