-
Notifications
You must be signed in to change notification settings - Fork 1
/
logformat.go
65 lines (58 loc) · 1.41 KB
/
logformat.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
53
54
55
56
57
58
59
60
61
62
63
64
65
package kid
import (
"github.com/gin-gonic/gin"
"io"
"net/http"
"time"
)
func LoggerWithFormatter(f LogFormatter) Middleware {
return func(ctx *Context) {
gin.LoggerWithConfig(gin.LoggerConfig{
Formatter: convertLogFormatter(f),
})
}
}
type LogFormatter func(params LogFormatterParams) string
type LogFormatterParams struct {
Request *http.Request
TimeStamp time.Time
StatusCode int
Latency time.Duration
ClientIP string
Method string
Path string
ErrorMessage string
isTerm bool
BodySize int
Keys map[string]interface{}
}
type LoggerConfig struct {
Formatter LogFormatter
Output io.Writer
SkipPaths []string
}
func LoggerWithConfig(conf LoggerConfig) Middleware {
return func(ctx *Context) {
gin.LoggerWithConfig(gin.LoggerConfig{
Formatter: convertLogFormatter(conf.Formatter),
Output: conf.Output,
SkipPaths: conf.SkipPaths,
})
}
}
func convertLogFormatter(formatter LogFormatter) gin.LogFormatter {
return func(params gin.LogFormatterParams) string {
return formatter(LogFormatterParams{
Request: params.Request,
TimeStamp: params.TimeStamp,
StatusCode: params.StatusCode,
Latency: params.Latency,
ClientIP: params.ClientIP,
Method: params.Method,
Path: params.Method,
ErrorMessage: params.ErrorMessage,
BodySize: params.BodySize,
Keys: params.Keys,
})
}
}