forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_helpers.go
69 lines (59 loc) · 1.54 KB
/
log_helpers.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
package main
import (
"net/http"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/request"
"github.com/TykTechnologies/tyk/config"
)
// identifies that field value was hidden before output to the log
const logHiddenValue = "<hidden>"
func obfuscateKey(keyName string) string {
if config.Global().EnableKeyLogging {
return keyName
}
if len(keyName) > 4 {
return "****" + keyName[len(keyName)-4:]
}
return "--"
}
func getLogEntryForRequest(logger *logrus.Entry, r *http.Request, key string, data map[string]interface{}) *logrus.Entry {
if logger == nil {
logger = logrus.NewEntry(log)
}
// populate http request fields
fields := logrus.Fields{
"path": r.URL.Path,
"origin": request.RealIP(r),
}
// add key to log if configured to do so
if key != "" {
fields["key"] = key
if !config.Global().EnableKeyLogging {
fields["key"] = obfuscateKey(key)
}
}
// add to log additional fields if any passed
for key, val := range data {
fields[key] = val
}
return logger.WithFields(fields)
}
func getExplicitLogEntryForRequest(logger *logrus.Entry, path string, IP string, key string, data map[string]interface{}) *logrus.Entry {
// populate http request fields
fields := logrus.Fields{
"path": path,
"origin": IP,
}
// add key to log if configured to do so
if key != "" {
fields["key"] = key
if !config.Global().EnableKeyLogging {
fields["key"] = logHiddenValue
}
}
// add to log additional fields if any passed
for key, val := range data {
fields[key] = val
}
return logger.WithFields(fields)
}