-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
108 lines (88 loc) · 2.63 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
package main
import (
"log"
"log/slog"
"net/http"
"strconv"
"time"
"github.com/jwalton/gchalk"
)
type StatusRecorder struct {
http.ResponseWriter
Status int
}
func (sr *StatusRecorder) WriteHeader(status int) {
sr.Status = status
sr.ResponseWriter.WriteHeader(status)
}
var LogLevel = new(slog.LevelVar)
func Warn(msg string, args ...any) {
slog.Warn(gchalk.Yellow(msg), args...)
}
func Error(msg string, args ...any) {
slog.Error(gchalk.Red(msg), args...)
}
func Debug(msg string, args ...any) {
slog.Debug(gchalk.Dim(msg), args...)
}
func LogRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
log.SetFlags(log.Ldate | log.Ltime)
log.SetPrefix("[amock]: ")
remoteAddr := gchalk.Bold(r.RemoteAddr)
method := RequestMethodColor(r.Method, true)
recorder := &StatusRecorder{w, http.StatusOK}
next.ServeHTTP(recorder, r)
elapsed := time.Since(start).String()
elapsed = gchalk.WithItalic().Dim("(" + elapsed + ")")
status := getStatusColor(recorder.Status)
// [amock]: 2024/04/01 02:43:10 - localhost:8000 | 127.0.0.1:12345 -> GET /api/v1/users - 200 OK (1.234s)
log.Printf("- %s | %s -> %s %s - %s %s", r.Host, remoteAddr, method, r.URL, status, elapsed)
})
}
func RequestMethodColor(m string, inverse bool) string {
method := m
if inverse {
method = " " + m + " "
}
switch m {
case http.MethodGet:
method = gchalk.WithBold().BrightBlue(method)
case http.MethodPost:
method = gchalk.WithBold().BrightGreen(method)
case http.MethodPut:
method = gchalk.WithBold().BrightYellow(method)
case http.MethodPatch:
method = gchalk.WithBold().BrightCyan(method)
case http.MethodDelete:
method = gchalk.WithBold().Red(method)
case http.MethodOptions:
method = gchalk.WithBold().Blue(method)
case http.MethodHead:
method = gchalk.WithBold().Magenta(method)
default:
method = m
}
if inverse {
method = gchalk.BgBrightWhite(method)
method = gchalk.Inverse(method)
}
return method
}
func getStatusColor(status int) string {
var color string
switch {
case status >= 200 && status < 300:
color = gchalk.BrightGreen(gchalk.Bold(strconv.Itoa(status)), http.StatusText(status))
case status >= 300 && status < 400:
color = gchalk.BrightBlue(gchalk.Bold(strconv.Itoa(status)), http.StatusText(status))
case status >= 400 && status < 500:
color = gchalk.BrightYellow(gchalk.Bold(strconv.Itoa(status)), http.StatusText(status))
case status >= 500:
color = gchalk.BrightRed(gchalk.Bold(strconv.Itoa(status)), http.StatusText(status))
default:
color = gchalk.Bold(strconv.Itoa(status)) + http.StatusText(status)
}
return color
}