-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
328 lines (275 loc) · 7.09 KB
/
log.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package server
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
const (
timeFormat = "2006-01-02 15:04:05.00"
)
func WriteLogStart(t time.Time) string {
return "\n /\\ /\\ /\\ /\\ /\\ /\\" +
"\n <> <> <> - [" + t.Format(timeFormat) + "] - SERVER ONLINE - <> <> <>" +
"\n \\/ \\/ \\/ \\/ \\/ \\/\n\n"
}
func WriteLogClosure(t time.Time) string {
return "\n /\\ /\\ /\\ /\\ /\\ /\\" +
"\n <> <> <> - [" + t.Format(timeFormat) + "] - SERVER OFFLINE - <> <> <>" +
"\n \\/ \\/ \\/ \\/ \\/ \\/\n\n"
}
func (router *Router) ClearLogs() {
oldFile := router.logFile
oldFile.Close()
router.logFile, _ = os.OpenFile(oldFile.Name(), os.O_TRUNC | os.O_CREATE | os.O_WRONLY | os.O_SYNC, 0777)
router.plainPrintf(WriteLogStart(router.startTime))
router.plainPrintf(" -- -- -- Logs cleared at [%s] -- -- --\n\n", time.Now().Format("02/Jan/2006:15:04:05"))
}
const (
httpInfoFormat = "%-16s - %-4s %-50s %s %d %10.3f MB - (%6d ms) \u279C %s (%s) via %s"
httpWarningFormat = "%-16s - %-4s %-50s %s %d %10.3f MB - (%6d ms) \u279C %s (%s) via %s \u279C %s"
httpErrorFormat = "%-16s - %-4s %-50s %s %d %10.3f MB - (%6d ms) \u279C %s (%s) via %s \u279C %s"
)
func (route *Route) logHTTPInfo(m metrics) {
lock := "\U0000274C"
if route.Secure {
lock = "\U0001F512"
}
route.Log(LOG_LEVEL_INFO, fmt.Sprintf(httpInfoFormat,
route.RemoteAddress,
route.R.Method,
route.logRequestURI,
lock,
m.Code,
(float64(m.Written)/1000000.),
m.Duration.Milliseconds(),
route.Website.Name,
route.Domain.Name,
route.Host,
))
}
func (route *Route) logHTTPWarning(m metrics) {
lock := "\U0000274C"
if route.Secure {
lock = "\U0001F512"
}
route.Log(LOG_LEVEL_WARNING, fmt.Sprintf(httpWarningFormat,
route.RemoteAddress,
route.R.Method,
route.logRequestURI,
lock,
m.Code,
(float64(m.Written)/1000000.),
m.Duration.Milliseconds(),
route.Website.Name,
route.Domain.Name,
route.Host,
route.logErrMessage,
))
}
func (route *Route) logHTTPError(m metrics) {
lock := "\U0000274C"
if route.Secure {
lock = "\U0001F512"
}
route.Log(LOG_LEVEL_ERROR, fmt.Sprintf(httpErrorFormat,
route.RemoteAddress,
route.R.Method,
route.logRequestURI,
lock,
m.Code,
(float64(m.Written)/1000000.),
m.Duration.Milliseconds(),
route.Website.Name,
route.Domain.Name,
route.Host,
route.logErrMessage,
))
}
func (route *Route) serveError() {
if route.W.hasWrote || route.errTemplate == nil {
return
}
if route.Method == "GET" || route.Method == "HEAD" {
data := struct{
Code int
Message string
}{
Code: route.W.code,
Message: route.errMessage,
}
var buf bytes.Buffer
if err := route.errTemplate.Execute(&buf, data); err != nil {
route.Log(LOG_LEVEL_ERROR, "Error serving template file", err)
return
}
route.ServeData(buf.Bytes())
return
}
route.ServeText(route.errMessage)
}
type panicError struct {
err error
panicErr error
stack string
}
// LogLevel defines the severity of a Log. See the constants
type LogLevel int
const (
LOG_LEVEL_INFO = iota
LOG_LEVEL_DEBUG
LOG_LEVEL_WARNING
LOG_LEVEL_ERROR
LOG_LEVEL_FATAL
)
func (l LogLevel) String() string {
switch l {
case LOG_LEVEL_INFO:
return " Info"
case LOG_LEVEL_DEBUG:
return " Debug"
case LOG_LEVEL_WARNING:
return "Warning"
case LOG_LEVEL_ERROR:
return " Error"
case LOG_LEVEL_FATAL:
return " Fatal"
default:
return " ??? "
}
}
// Log is the structure that can be will store any log reported
// with Logger. It keeps the error severity level (see the constants)
// the date it was created and the message associated with it (probably
// an error). It also has the optional field "extra" that can be used to
// store additional information
type Log struct {
id string
Level LogLevel
Date time.Time
Message string
Extra string
}
// JSON returns the Log l in a json-encoded string in form of a
// slice of bytes
func (l Log) JSON() []byte {
jsonL := struct {
ID string `json:"id"`
Level string `json:"level"`
Date time.Time `json:"date"`
Message string `json:"message"`
Extra string `json:"extra"`
}{
l.id,
strings.TrimSpace(l.Level.String()), l.Date,
l.Message, l.Extra,
}
b, _ := json.Marshal(jsonL)
return b
}
func (l Log) String() string {
return fmt.Sprintf(
"[%v] - %v: %s",
l.Date.Format(timeFormat),
l.Level, l.Message,
)
}
func (l Log) Full() string {
if l.Extra == "" {
return l.String()
}
return fmt.Sprintf(
"[%v] - %v: %s -> %s",
l.Date.Format(timeFormat),
l.Level, l.Message,
l.Extra,
)
}
// Logger is used by the Router and can be used by the user to
// create logs that are both written to the chosen io.Writer (if any)
// and saved locally in memory, so that they can be retreived
// programmatically and used (for example to make a view in a website)
type Logger interface {
Log(level LogLevel, message string, extra ...any)
Logs() []Log
JSON() []byte
}
// Logs returns the list of logs stored
func (router *Router) Logs() []Log {
logs := make([]Log, 0, len(router.logs))
logs = append(logs, router.logs...)
return logs
}
// JSON returns the list of logs stored in JSON format (see Log.JSON() method)
func (router *Router) JSON() []byte {
res := make([]byte, 0)
res = append(res, []byte("[")...)
first := true
for _, log := range router.logs {
if !first {
res = append(res, []byte(",")...)
} else {
first = false
}
res = append(res, log.JSON()...)
}
res = append(res, []byte("]")...)
return res
}
// Router
func (router *Router) Log(level LogLevel, message string, extra ...any) {
t := time.Now()
router.mLog.Lock()
defer router.mLog.Unlock()
log := Log {
fmt.Sprintf(
"%02d%02d%02d%02d%02d%02d%03d",
t.Year() % 100, t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second(), rand.Intn(1000),
), level, t,
message, fmt.Sprint(extra...),
}
if router.logFile != nil {
if log.Extra != "" {
fmt.Fprintf(router.logFile, "%v\n%s\n", log, IndentString(log.Extra, 1))
} else {
fmt.Fprintln(router.logFile, log)
}
}
}
func (router *Router) Print(a ...any) {
var v []string
for _, el := range a {
v = append(v, fmt.Sprint(el))
}
router.Log(LOG_LEVEL_DEBUG, strings.Join(v, " "))
}
func (router *Router) Printf(format string, a ...any) {
router.Log(LOG_LEVEL_DEBUG, fmt.Sprintf(format, a...))
}
func (router *Router) plainPrintf(format string, a ...any) {
fmt.Fprintf(router.logFile, format, a...)
}
// Server
func (srv *Server) Log(level LogLevel, message string, a ...any) {
srv.Router.Log(level, message, a...)
}
func (srv *Server) Print(a ...any) {
srv.Router.Print(a...)
}
func (srv *Server) Printf(format string, a ...any) {
srv.Router.Printf(format, a...)
}
// Route
func (route *Route) Log(level LogLevel, message string, a ...any) {
route.Srv.Log(level, message, a...)
}
func (route *Route) Print(a ...any) {
route.Srv.Print(a...)
}
func (route *Route) Printf(format string, a ...any) {
route.Srv.Printf(format, a...)
}