-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.go
145 lines (122 loc) · 3.21 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
/*
* Copyright (c) Fabio da Silva Ribeiro <[email protected]>
* SPDX-License-Identifier: MIT
*/
package middleware
import (
"strconv"
"strings"
"time"
"github.com/labstack/echo/v4"
)
// Log middlewares constants.
const (
logID = "@id"
logRemoteIP = "@remote_ip"
logURI = "@uri"
logHost = "@host"
logMethod = "@method"
logPath = "@path"
logRoute = "@route"
logProtocol = "@protocol"
logReferer = "@referer"
logUserAgent = "@user_agent"
logStatus = "@status"
logError = "@error"
logLatency = "@latency"
logLatencyHuman = "@latency_human"
logBytesIn = "@bytes_in"
logBytesOut = "@bytes_out"
logHeaderPrefix = "@header:"
logQueryPrefix = "@query:"
logFormPrefix = "@form:"
logCookiePrefix = "@cookie:"
)
var defaultFields = map[string]string{
"remote_ip": logRemoteIP,
"uri": logURI,
"host": logHost,
"method": logMethod,
"status": logStatus,
"latency": logLatency,
"error": logError,
}
// string to int base conversion.
const base = 10
// mapFields maps fields based on tag name.
func mapFields(ec echo.Context, h echo.HandlerFunc, fm map[string]string) (map[string]interface{}, error) {
logFields := map[string]interface{}{}
start := time.Now()
err := h(ec)
if err != nil {
ec.Error(err)
}
elapsed := time.Since(start)
tags := mapTags(ec, elapsed)
if err != nil {
tags[logError] = err
}
for k, tag := range fm {
if tag == "" {
continue
}
if value, ok := tags[tag]; ok {
logFields[k] = value
continue
}
switch {
case strings.HasPrefix(tag, logHeaderPrefix):
key := tag[len(logHeaderPrefix):]
logFields[k] = ec.Request().Header.Get(key)
case strings.HasPrefix(tag, logQueryPrefix):
key := tag[len(logQueryPrefix):]
logFields[k] = ec.QueryParam(key)
case strings.HasPrefix(tag, logFormPrefix):
key := tag[len(logFormPrefix):]
logFields[k] = ec.FormValue(key)
case strings.HasPrefix(tag, logCookiePrefix):
key := tag[len(logCookiePrefix):]
cookie, err := ec.Cookie(key)
if err == nil {
logFields[k] = cookie.Value
}
}
}
return logFields, err
}
// mapTags maps the log tags with its related data. Populate previously the
// key/value avoids the cyclomatic complexity of the log middlewares to
// identify each tag and value.
func mapTags(ec echo.Context, latency time.Duration) map[string]interface{} {
tags := map[string]interface{}{}
req := ec.Request()
res := ec.Response()
id := req.Header.Get(echo.HeaderXRequestID)
if id == "" {
id = res.Header().Get(echo.HeaderXRequestID)
}
tags[logID] = id
tags[logRemoteIP] = ec.RealIP()
tags[logURI] = req.RequestURI
tags[logHost] = req.Host
tags[logMethod] = req.Method
path := req.URL.Path
if path == "" {
path = "/"
}
tags[logPath] = path
tags[logRoute] = ec.Path()
tags[logProtocol] = req.Proto
tags[logReferer] = req.Referer()
tags[logUserAgent] = req.UserAgent()
tags[logStatus] = res.Status
tags[logLatency] = strconv.FormatInt(int64(latency), base)
tags[logLatencyHuman] = latency.String()
cl := req.Header.Get(echo.HeaderContentLength)
if cl == "" {
cl = "0"
}
tags[logBytesIn] = cl
tags[logBytesOut] = strconv.FormatInt(res.Size, base)
return tags
}