-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
logger.go
297 lines (271 loc) · 6.96 KB
/
logger.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package middleware
import (
"bytes"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/gofiber/fiber"
"github.com/gofiber/utils"
"github.com/valyala/bytebufferpool"
)
// Middleware types
type (
// LoggerConfig defines the config for Logger middleware.
LoggerConfig struct {
// Next defines a function to skip this middleware.
Next func(ctx *fiber.Ctx) bool
// Format defines the logging tags
//
// - time
// - ip
// - ips
// - url
// - host
// - method
// - path
// - protocol
// - route
// - referer
// - ua
// - latency
// - status
// - body
// - error
// - bytesSent
// - bytesReceived
// - header:<key>
// - query:<key>
// - form:<key>
// - cookie:<key>
//
// Optional. Default: ${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n
Format string
// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
//
// Optional. Default: 15:04:05
TimeFormat string
// Output is a writter where logs are written
//
// Default: os.Stderr
Output io.Writer
}
)
// Logger variables
const (
LoggerTagTime = "time"
LoggerTagReferer = "referer"
LoggerTagProtocol = "protocol"
LoggerTagIP = "ip"
LoggerTagIPs = "ips"
LoggerTagHost = "host"
LoggerTagMethod = "method"
LoggerTagPath = "path"
LoggerTagURL = "url"
LoggerTagUA = "ua"
LoggerTagLatency = "latency"
LoggerTagStatus = "status"
LoggerTagBody = "body"
LoggerTagBytesSent = "bytesSent"
LoggerTagBytesReceived = "bytesReceived"
LoggerTagRoute = "route"
LoggerTagError = "error"
LoggerTagHeader = "header:"
LoggerTagQuery = "query:"
LoggerTagForm = "form:"
LoggerTagCookie = "cookie:"
)
// LoggerConfigDefault is the default config
var LoggerConfigDefault = LoggerConfig{
Next: nil,
Format: "${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n",
TimeFormat: "15:04:05",
Output: os.Stderr,
}
// Logger is the default initiator allowing to pass a log format
func Logger(format ...string) fiber.Handler {
// Create default config
var config = LoggerConfigDefault
// Set format if provided
if len(format) > 0 {
config.Format = format[0]
}
// Return LoggerWithConfig
return LoggerWithConfig(config)
}
// LoggerWithConfig allows you to pass an CompressConfig
func LoggerWithConfig(config LoggerConfig) fiber.Handler {
// Middleware settings
var mutex sync.RWMutex
var tmpl loggerTemplate
tmpl.new(config.Format, "${", "}")
timestamp := time.Now().Format(config.TimeFormat)
// Update date/time every second in a separate go routine
if strings.Contains(config.Format, "${time}") {
go func() {
for {
mutex.Lock()
timestamp = time.Now().Format(config.TimeFormat)
mutex.Unlock()
time.Sleep(250 * time.Millisecond)
}
}()
}
// Return handler
return func(c *fiber.Ctx) {
// Don't execute the middleware if Next returns true
if config.Next != nil && config.Next(c) {
c.Next()
return
}
// Middleware logic...
start := time.Now()
// handle request
c.Next()
// build log
stop := time.Now()
// Get new buffer
buf := bytebufferpool.Get()
_, err := tmpl.executeFunc(buf, func(w io.Writer, tag string) (int, error) {
switch tag {
case LoggerTagTime:
mutex.RLock()
defer mutex.RUnlock()
return buf.WriteString(timestamp)
case LoggerTagReferer:
return buf.WriteString(c.Get(fiber.HeaderReferer))
case LoggerTagProtocol:
return buf.WriteString(c.Protocol())
case LoggerTagIP:
return buf.WriteString(c.IP())
case LoggerTagIPs:
return buf.WriteString(c.Get(fiber.HeaderXForwardedFor))
case LoggerTagHost:
return buf.WriteString(c.Hostname())
case LoggerTagMethod:
return buf.WriteString(c.Method())
case LoggerTagPath:
return buf.WriteString(c.Path())
case LoggerTagURL:
return buf.WriteString(c.OriginalURL())
case LoggerTagUA:
return buf.WriteString(c.Get(fiber.HeaderUserAgent))
case LoggerTagLatency:
return buf.WriteString(stop.Sub(start).String())
case LoggerTagStatus:
return buf.WriteString(strconv.Itoa(c.Fasthttp.Response.StatusCode()))
case LoggerTagBody:
return buf.WriteString(c.Body())
case LoggerTagBytesReceived:
return buf.WriteString(strconv.Itoa(len(c.Fasthttp.Request.Body())))
case LoggerTagBytesSent:
return buf.WriteString(strconv.Itoa(len(c.Fasthttp.Response.Body())))
case LoggerTagRoute:
return buf.WriteString(c.Route().Path)
case LoggerTagError:
if c.Error() != nil {
return buf.WriteString(c.Error().Error())
}
default:
switch {
case strings.HasPrefix(tag, LoggerTagHeader):
return buf.WriteString(c.Get(tag[7:]))
case strings.HasPrefix(tag, LoggerTagQuery):
return buf.WriteString(c.Query(tag[6:]))
case strings.HasPrefix(tag, LoggerTagForm):
return buf.WriteString(c.FormValue(tag[5:]))
case strings.HasPrefix(tag, LoggerTagCookie):
return buf.WriteString(c.Cookies(tag[7:]))
}
}
return 0, nil
})
if err != nil {
_, _ = buf.WriteString(err.Error())
}
if _, err := config.Output.Write(buf.Bytes()); err != nil {
fmt.Println(err)
}
bytebufferpool.Put(buf)
}
}
// MIT License fasttemplate
// Copyright (c) 2015 Aliaksandr Valialkin
// https://github.com/valyala/fasttemplate/blob/master/LICENSE
type (
loggerTemplate struct {
template string
startTag string
endTag string
texts [][]byte
tags []string
}
loggerTagFunc func(w io.Writer, tag string) (int, error)
)
func (t *loggerTemplate) new(template, startTag, endTag string) {
t.template = template
t.startTag = startTag
t.endTag = endTag
t.texts = t.texts[:0]
t.tags = t.tags[:0]
if len(startTag) == 0 {
panic("startTag cannot be empty")
}
if len(endTag) == 0 {
panic("endTag cannot be empty")
}
s := utils.GetBytes(template)
a := utils.GetBytes(startTag)
b := utils.GetBytes(endTag)
tagsCount := bytes.Count(s, a)
if tagsCount == 0 {
return
}
if tagsCount+1 > cap(t.texts) {
t.texts = make([][]byte, 0, tagsCount+1)
}
if tagsCount > cap(t.tags) {
t.tags = make([]string, 0, tagsCount)
}
for {
n := bytes.Index(s, a)
if n < 0 {
t.texts = append(t.texts, s)
break
}
t.texts = append(t.texts, s[:n])
s = s[n+len(a):]
n = bytes.Index(s, b)
if n < 0 {
panic(fmt.Errorf("cannot find end tag=%q in the template=%q starting from %q", endTag, template, s))
}
t.tags = append(t.tags, utils.GetString(s[:n]))
s = s[n+len(b):]
}
}
func (t *loggerTemplate) executeFunc(w io.Writer, f loggerTagFunc) (int64, error) {
var nn int64
n := len(t.texts) - 1
if n == -1 {
ni, err := w.Write(utils.GetBytes(t.template))
return int64(ni), err
}
for i := 0; i < n; i++ {
ni, err := w.Write(t.texts[i])
nn += int64(ni)
if err != nil {
return nn, err
}
ni, err = f(w, t.tags[i])
nn += int64(ni)
if err != nil {
return nn, err
}
}
ni, err := w.Write(t.texts[n])
nn += int64(ni)
return nn, err
}