-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzflogger.go
114 lines (97 loc) · 2.38 KB
/
zflogger.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
package zflogger
import (
"fmt"
"net/http"
"runtime/debug"
"time"
"github.com/gofiber/fiber"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
type logFields struct {
ID string
RemoteIP string
Host string
Method string
Path string
Protocol string
StatusCode int
Latency float64
Error error
Stack []byte
}
func (lf *logFields) MarshalZerologObject(e *zerolog.Event) {
e.
Str("id", lf.ID).
Str("remote_ip", lf.RemoteIP).
Str("host", lf.Host).
Str("method", lf.Method).
Str("path", lf.Path).
Str("protocol", lf.Protocol).
Int("status_code", lf.StatusCode).
Float64("latency", lf.Latency).
Str("tag", "request")
if lf.Error != nil {
e.Err(lf.Error)
}
if lf.Stack != nil {
e.Bytes("stack", lf.Stack)
}
}
//Middleware requestid + logger + recover for request traceability
func Middleware(log zerolog.Logger, filter func(*fiber.Ctx) bool) func(*fiber.Ctx) {
return func(c *fiber.Ctx) {
if filter != nil && filter(c) {
c.Next()
return
}
start := time.Now()
rid := c.Get(fiber.HeaderXRequestID)
if rid == "" {
rid = uuid.New().String()
c.Set(fiber.HeaderXRequestID, rid)
}
fields := &logFields{
ID: rid,
RemoteIP: c.IP(),
Method: c.Method(),
Host: c.Hostname(),
Path: c.Path(),
Protocol: c.Protocol(),
}
defer func() {
rvr := recover()
if rvr != nil {
err, ok := rvr.(error)
if !ok {
err = fmt.Errorf("%v", rvr)
}
fields.Error = err
fields.Stack = debug.Stack()
c.Status(http.StatusInternalServerError)
c.JSON(map[string]interface{}{
"status": http.StatusText(http.StatusInternalServerError),
})
}
fields.StatusCode = c.Fasthttp.Response.StatusCode()
fields.Latency = time.Since(start).Seconds()
switch {
case rvr != nil:
log.Error().EmbedObject(fields).Msg("panic recover")
case fields.StatusCode >= 500:
log.Error().EmbedObject(fields).Msg("server error")
case fields.StatusCode >= 400:
log.Error().EmbedObject(fields).Msg("client error")
case fields.StatusCode >= 300:
log.Warn().EmbedObject(fields).Msg("redirect")
case fields.StatusCode >= 200:
log.Info().EmbedObject(fields).Msg("success")
case fields.StatusCode >= 100:
log.Info().EmbedObject(fields).Msg("informative")
default:
log.Warn().EmbedObject(fields).Msg("unknown status")
}
}()
c.Next()
}
}