forked from adamhassel/loggers-mapper-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic.go
77 lines (64 loc) · 2.02 KB
/
newrelic.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
// Package newrelic maps a birkirb/loggers to a newrelic.Logger
package newrelic
import (
"fmt"
"regexp"
"github.com/Vivino/go-loggers"
"github.com/Vivino/go-loggers/mappers/stdlib"
newrelic "github.com/newrelic/go-agent"
)
// Logger is an Contextual logger wrapper over Logrus's logger.
type Logger struct {
l loggers.Contextual
debug bool
censor bool
}
// NewLogger converts birkirb's logger to a New Relic compatible logger. If censor, any new relic license is redacted from logs
func NewLogger(logger loggers.Contextual, debug, censor bool) newrelic.Logger {
return &Logger{
l: logger,
debug: debug,
censor: censor,
}
}
// NewDefaultLogger returns a default Contextual Logger for birkirb's logger.
func NewDefaultLogger() newrelic.Logger {
l := stdlib.NewDefaultLogger()
return NewLogger(l, false, true)
}
// Error logs newrelic errors
func (l Logger) Error(msg string, fields map[string]interface{}) {
l.l.WithFields(l.convert(fields)...).Error(msg)
}
// Warn logs newrelic warnings
func (l Logger) Warn(msg string, fields map[string]interface{}) {
l.l.WithFields(l.convert(fields)...).Warn(msg)
}
// Info logs newrelic info messages
func (l Logger) Info(msg string, fields map[string]interface{}) {
l.l.WithFields(l.convert(fields)...).Info(msg)
}
// Debug logs newrelic debug messages
func (l Logger) Debug(msg string, fields map[string]interface{}) {
l.l.WithFields(l.convert(fields)...).Debug(msg)
}
// DebugEnabled returns whether debug is on
func (l Logger) DebugEnabled() bool {
return l.debug
}
var filterRegEx = regexp.MustCompile(`[0-9a-z]+`)
var errorRegEx = regexp.MustCompile(`license_key=[0-9]{40}`)
func (l Logger) convert(c map[string]interface{}) []interface{} {
output := make([]interface{}, 0, 2*len(c))
for k, v := range c {
s := fmt.Sprint(v)
if k == "license_key" && l.censor {
s = filterRegEx.ReplaceAllString(s, "[REDACTED]")
}
if k == "error" && l.censor {
s = errorRegEx.ReplaceAllString(s, "license_key=[REDACTED]")
}
output = append(output, k, s)
}
return output
}