forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
267 lines (229 loc) · 6.11 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
package logging
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"net/http"
"os"
"strings"
"time"
)
var Logger *logrus.Logger
// The of cookies which should not be logged
var AccessLogCookiesBlacklist = []string{}
var LifecycleEnvVars = []string{"BUILD_NUMBER", "BUILD_HASH", "BUILD_DATE"}
func init() {
Set("info", false)
}
// Set creates a new Logger with the matching specification
func Set(level string, textLogging bool) error {
l, err := logrus.ParseLevel(level)
if err != nil {
return err
}
logger := logrus.New()
if textLogging {
logger.Formatter = &logrus.TextFormatter{}
} else {
logger.Formatter = &LogstashFormatter{TimestampFormat: time.RFC3339Nano}
}
logger.Level = l
Logger = logger
return nil
}
// Access logs an access entry with call duration and status code
func Access(r *http.Request, start time.Time, statusCode int) {
e := access(r, start, statusCode, nil)
var msg string
if len(r.URL.RawQuery) == 0 {
msg = fmt.Sprintf("%v ->%v %v", statusCode, r.Method, r.URL.Path)
} else {
msg = fmt.Sprintf("%v ->%v %v?...", statusCode, r.Method, r.URL.Path)
}
if statusCode >= 200 && statusCode <= 399 {
e.Info(msg)
} else if statusCode >= 400 && statusCode <= 499 {
e.Warn(msg)
} else {
e.Error(msg)
}
}
// AccessError logs an error while accessing
func AccessError(r *http.Request, start time.Time, err error) {
e := access(r, start, 0, err)
e.Errorf("ERROR ->%v %v", r.Method, r.URL.Path)
}
func access(r *http.Request, start time.Time, statusCode int, err error) *logrus.Entry {
url := r.URL.Path
if r.URL.RawQuery != "" {
url += "?" + r.URL.RawQuery
}
fields := logrus.Fields{
"type": "access",
"@timestamp": start,
"remote_ip": getRemoteIp(r),
"host": r.Host,
"url": url,
"method": r.Method,
"proto": r.Proto,
"duration": time.Since(start).Nanoseconds() / 1000000,
"User_Agent": r.Header.Get("User-Agent"),
}
if statusCode != 0 {
fields["response_status"] = statusCode
}
if err != nil {
fields[logrus.ErrorKey] = err.Error()
}
setCorrelationIds(fields, r.Header)
cookies := map[string]string{}
for _, c := range r.Cookies() {
if !contains(AccessLogCookiesBlacklist, c.Name) {
cookies[c.Name] = c.Value
}
}
if len(cookies) > 0 {
fields["cookies"] = cookies
}
return Logger.WithFields(fields)
}
// Call logs the result of an outgoing call
func Call(r *http.Request, resp *http.Response, start time.Time, err error) {
url := r.URL.Path
if r.URL.RawQuery != "" {
url += "?" + r.URL.RawQuery
}
fields := logrus.Fields{
"type": "call",
"@timestamp": start,
"host": r.Host,
"url": url,
"full_url": r.URL.String(),
"method": r.Method,
"duration": time.Since(start).Nanoseconds() / 1000000,
}
setCorrelationIds(fields, r.Header)
if err != nil {
fields[logrus.ErrorKey] = err.Error()
Logger.WithFields(fields).Error(err)
return
}
if resp != nil {
fields["response_status"] = resp.StatusCode
fields["content_type"] = resp.Header.Get("Content-Type")
e := Logger.WithFields(fields)
msg := fmt.Sprintf("%v %v-> %v", resp.StatusCode, r.Method, r.URL.String())
if resp.StatusCode >= 200 && resp.StatusCode <= 399 {
e.Info(msg)
} else if resp.StatusCode >= 400 && resp.StatusCode <= 499 {
e.Warn(msg)
} else {
e.Error(msg)
}
return
}
Logger.WithFields(fields).Warn("call, but no response given")
}
// Cacheinfo logs the hit information a accessing a ressource
func Cacheinfo(url string, hit bool) {
var msg string
if hit {
msg = fmt.Sprintf("cache hit: %v", url)
} else {
msg = fmt.Sprintf("cache miss: %v", url)
}
Logger.WithFields(
logrus.Fields{
"type": "cacheinfo",
"url": url,
"hit": hit,
}).
Debug(msg)
}
// Return a log entry for application logs,
// prefilled with the correlation ids out of the supplied request.
func Application(h http.Header) *logrus.Entry {
fields := logrus.Fields{
"type": "application",
}
setCorrelationIds(fields, h)
return Logger.WithFields(fields)
}
// LifecycleStart logs the start of an application
// with the configuration struct or map as paramter.
func LifecycleStart(appName string, args interface{}) {
fields := logrus.Fields{}
jsonString, err := json.Marshal(args)
if err == nil {
err := json.Unmarshal(jsonString, &fields)
if err != nil {
fields["parse_error"] = err.Error()
}
}
fields["type"] = "lifecycle"
fields["event"] = "start"
for _, env := range LifecycleEnvVars {
if os.Getenv(env) != "" {
fields[strings.ToLower(env)] = os.Getenv(env)
}
}
Logger.WithFields(fields).Infof("starting application: %v", appName)
}
// LifecycleStop logs the stop of an application
func LifecycleStop(appName string, signal os.Signal, err error) {
fields := logrus.Fields{
"type": "lifecycle",
"event": "stop",
}
if signal != nil {
fields["signal"] = signal.String()
}
if os.Getenv("BUILD_NUMBER") != "" {
fields["build_number"] = os.Getenv("BUILD_NUMBER")
}
if err != nil {
Logger.WithFields(fields).
WithError(err).
Errorf("stopping application: %v (%v)", appName, err)
} else {
Logger.WithFields(fields).Infof("stopping application: %v (%v)", appName, signal)
}
}
// LifecycleStop logs the stop of an application
func ServerClosed(appName string) {
fields := logrus.Fields{
"type": "application",
"event": "stop",
}
if os.Getenv("BUILD_NUMBER") != "" {
fields["build_number"] = os.Getenv("BUILD_NUMBER")
}
Logger.WithFields(fields).Infof("http server was closed: %v", appName)
}
func getRemoteIp(r *http.Request) string {
if r.Header.Get("X-Cluster-Client-Ip") != "" {
return r.Header.Get("X-Cluster-Client-Ip")
}
if r.Header.Get("X-Real-Ip") != "" {
return r.Header.Get("X-Real-Ip")
}
return strings.Split(r.RemoteAddr, ":")[0]
}
func setCorrelationIds(fields logrus.Fields, h http.Header) {
correlationId := GetCorrelationId(h)
if correlationId != "" {
fields["correlation_id"] = correlationId
}
userCorrelationId := GetUserCorrelationId(h)
if userCorrelationId != "" {
fields["user_correlation_id"] = userCorrelationId
}
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}