github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/log_helpers.go (about) 1 package gateway 2 3 import ( 4 "net/http" 5 6 "github.com/sirupsen/logrus" 7 8 "github.com/TykTechnologies/tyk/request" 9 10 "github.com/TykTechnologies/tyk/config" 11 ) 12 13 // identifies that field value was hidden before output to the log 14 const logHiddenValue = "<hidden>" 15 16 func obfuscateKey(keyName string) string { 17 if config.Global().EnableKeyLogging { 18 return keyName 19 } 20 21 if len(keyName) > 4 { 22 return "****" + keyName[len(keyName)-4:] 23 } 24 return "--" 25 } 26 27 func getLogEntryForRequest(logger *logrus.Entry, r *http.Request, key string, data map[string]interface{}) *logrus.Entry { 28 if logger == nil { 29 logger = logrus.NewEntry(log) 30 } 31 32 // populate http request fields 33 fields := logrus.Fields{ 34 "path": r.URL.Path, 35 "origin": request.RealIP(r), 36 } 37 // add key to log if configured to do so 38 if key != "" { 39 fields["key"] = key 40 if !config.Global().EnableKeyLogging { 41 fields["key"] = obfuscateKey(key) 42 } 43 } 44 // add to log additional fields if any passed 45 for key, val := range data { 46 fields[key] = val 47 } 48 return logger.WithFields(fields) 49 } 50 51 func getExplicitLogEntryForRequest(logger *logrus.Entry, path string, IP string, key string, data map[string]interface{}) *logrus.Entry { 52 // populate http request fields 53 fields := logrus.Fields{ 54 "path": path, 55 "origin": IP, 56 } 57 // add key to log if configured to do so 58 if key != "" { 59 fields["key"] = key 60 if !config.Global().EnableKeyLogging { 61 fields["key"] = logHiddenValue 62 } 63 } 64 // add to log additional fields if any passed 65 for key, val := range data { 66 fields[key] = val 67 } 68 return logger.WithFields(fields) 69 }