-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
53 lines (48 loc) · 1.11 KB
/
logger.go
File metadata and controls
53 lines (48 loc) · 1.11 KB
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
package fin
import (
"log"
"os"
"strings"
"time"
"github.com/valyala/fasthttp"
)
var logger *log.Logger
func SimpleLogger() HandlerFunc {
return func(c *Context) {
if logger == nil {
logger = log.New(os.Stdout, "[fin]", 0)
}
start := time.Now()
c.Next()
end := time.Now()
logger.Printf("%v | %3d | %8v | %15s |%-7s %#v\n",
end.Format("2006/01/02 - 15:04:05"),
c.Response.StatusCode(),
end.Sub(start),
c.RemoteIP(),
string(c.Method()),
string(c.Path()),
)
}
}
func Logger() HandlerFunc {
return func(c *Context) {
start := time.Now()
c.Next()
end := time.Now()
kvs := []interface{}{
"method", string(c.Method()),
"path", string(c.Path()),
"time", end.Format("2006/01/02 15:04:05"),
"status", c.Response.StatusCode(),
"cost", end.Sub(start),
"remote_ip", c.RemoteIP(),
"request_body", string(c.PostBody()),
}
contentType := string(c.Response.Header.Peek(fasthttp.HeaderContentType))
if strings.Contains(contentType, "application/json") {
kvs = append(kvs, "response", string(c.Response.Body()))
}
c.engine.logger.Infow("http request with", kvs...)
}
}