forked from AubSs/fasthttplogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthttplogger.go
160 lines (145 loc) · 4.12 KB
/
fasthttplogger.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package fasthttplogger
import (
"log"
"os"
"strconv"
"time"
"github.com/valyala/fasthttp"
)
var (
output = log.New(os.Stdout, "", 0)
)
var (
green = string([]byte{27, 91, 48, 48, 58, 51, 50, 109})
yellow = string([]byte{27, 91, 48, 48, 59, 51, 51, 109})
red = string([]byte{27, 91, 48, 48, 59, 51, 49, 109})
blue = string([]byte{27, 91, 48, 48, 59, 51, 52, 109})
white = string([]byte{27, 91, 48, 109})
)
func getColorByStatus(code int) string {
switch {
case code >= 200 && code < 300:
return green
case code >= 300 && code < 400:
return blue
case code >= 400 && code < 500:
return yellow
default:
return red
}
}
func colorStatus(code int) string {
return getColorByStatus(code) + strconv.Itoa(code) + white
}
func colorMethod(method []byte, code int) string {
return getColorByStatus(code) + string(method) + white
}
func getHttp(ctx *fasthttp.RequestCtx) string {
if ctx.Response.Header.IsHTTP11() {
return "HTTP/1.1"
}
return "HTTP/1.0"
}
/* ========================== Predefined Formats =========================== */
// Tiny format:
// <method> <url> - <status> - <response-time us>
// GET / - 200 - 11.925 us
func Tiny(req fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
begin := time.Now()
req(ctx)
end := time.Now()
output.Printf("%s %s - %v - %v",
ctx.Method(),
ctx.RequestURI(),
ctx.Response.Header.StatusCode(),
end.Sub(begin),
)
})
}
// TinyColored is same as Tiny but colored
func TinyColored(req fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
begin := time.Now()
req(ctx)
end := time.Now()
output.Printf("%s %s - %v - %v",
colorMethod(ctx.Method(), ctx.Response.Header.StatusCode()),
ctx.RequestURI(),
colorStatus(ctx.Response.Header.StatusCode()),
end.Sub(begin),
)
})
}
// Short format:
// <remote-addr> | <HTTP/:http-version> | <method> <url> - <status> - <response-time us>
// 127.0.0.1:53324 | HTTP/1.1 | GET /hello - 200 - 44.8µs
func Short(req fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
begin := time.Now()
req(ctx)
end := time.Now()
output.Printf("%v | %s | %s %s - %v - %v",
ctx.RemoteAddr(),
getHttp(ctx),
ctx.Method(),
ctx.RequestURI(),
ctx.Response.Header.StatusCode(),
end.Sub(begin),
)
})
}
// ShortColored is same as Short but colored
func ShortColored(req fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
begin := time.Now()
req(ctx)
end := time.Now()
output.Printf("%v | %s | %s %s - %v - %v",
ctx.RemoteAddr(),
getHttp(ctx),
colorMethod(ctx.Method(), ctx.Response.Header.StatusCode()),
ctx.RequestURI(),
colorStatus(ctx.Response.Header.StatusCode()),
end.Sub(begin),
)
})
}
// Combined format:
// [<time>] <remote-addr> | <HTTP/http-version> | <method> <url> - <status> - <response-time us> | <user-agent>
// [2017/05/31 - 13:27:28] 127.0.0.1:54082 | HTTP/1.1 | GET /hello - 200 - 48.279µs | Paw/3.1.1 (Macintosh; OS X/10.12.5) GCDHTTPRequest
func Combined(req fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
begin := time.Now()
req(ctx)
end := time.Now()
output.Printf("[%v] %v | %s | %v | %s %s - %v | %s",
end.Format("2006/01/02 - 15:04:05"),
ctx.RemoteAddr(),
getHttp(ctx),
end.Sub(begin),
ctx.Method(),
ctx.RequestURI(),
ctx.Response.Header.StatusCode(),
ctx.UserAgent(),
)
})
}
// CombinedColored is same as Combined but colored
func CombinedColored(req fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
begin := time.Now()
req(ctx)
end := time.Now()
output.Printf("[%v] %v | %s | %v | %s %s - %v | %s",
end.Format("2006/01/02 - 15:04:05"),
ctx.RemoteAddr(),
getHttp(ctx),
end.Sub(begin),
colorMethod(ctx.Method(), ctx.Response.Header.StatusCode()),
ctx.RequestURI(),
colorStatus(ctx.Response.Header.StatusCode()),
ctx.UserAgent(),
)
})
}