-
Notifications
You must be signed in to change notification settings - Fork 26
/
es_logger.go
79 lines (63 loc) · 1.88 KB
/
es_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
package util
import (
"fmt"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
type WrapKitLoggerDebug struct {
log.Logger
}
func (logger WrapKitLoggerDebug) Printf(format string, vars ...interface{}) {
cleanSenstiveData(&vars)
log.Debugln("[ElasticSearch: Trace] => ", fmt.Sprintf(format, vars...))
}
type WrapKitLoggerError struct {
log.Logger
}
func (logger WrapKitLoggerError) Printf(format string, vars ...interface{}) {
cleanSenstiveData(&vars)
formattedStr := fmt.Sprintf(format, vars...)
if DebugDeprecationWarns(formattedStr) {
return
}
log.Errorln("[ElasticSearch: Error] => ", formattedStr)
}
// DebugDeprecationWarns converts all the error logs containing
// deprecation warnings to debug logs so that it doesn't invoke sentry
func DebugDeprecationWarns(formattedStr string) bool {
// Check if any of the vars contain `deprecation` in it.
isDeprecated, _ := regexp.MatchString(`.*deprecation.*`, strings.ToLower(formattedStr))
if isDeprecated {
log.Debugln("[ElasticSearch: Trace] => ", formattedStr)
return true
}
return false
}
// cleanSenstiveData cleans credentials from the
// variables, if any.
func cleanSenstiveData(vars *[]interface{}) {
// Check if any var contains an URL, if it does, replace auth from the URL
for index, passedVar := range *vars {
// Cast the interface to a string
stringedVar, ok := passedVar.(string)
if !ok {
continue
}
cleanedVar := CleanPasswordFromURL(stringedVar)
(*vars)[index] = cleanedVar
}
}
// CleanPasswordFromURL will clean password from the URL if
// it is present
func CleanPasswordFromURL(URL string) string {
// Check if URL
isURL, _ := regexp.MatchString(`^https?://(www.)?.+\..+$`, URL)
if !isURL {
return URL
}
// If it is an URL, clean it up
cleanerRe := regexp.MustCompile(`\/\/(?P<username>.+):.+@`)
cleanedVar := cleanerRe.ReplaceAllString(URL, "//${username}:********@")
return cleanedVar
}