-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
158 lines (131 loc) · 2.96 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
package profiler
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/rs/zerolog"
dd_trace "gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
)
const (
DefaultLogLevel = zerolog.ErrorLevel
)
var (
logLevels = map[int]zerolog.Level{
4: zerolog.DebugLevel,
3: zerolog.InfoLevel,
2: zerolog.WarnLevel,
1: zerolog.ErrorLevel,
}
log zerolog.Logger
logRecorder logrecorder
)
func logLevel(v int) zerolog.Level {
if v < 1 {
v = 1
} else if v > 4 {
v = 4
}
return logLevels[v]
}
func setGlobalLogger(logger zerolog.Logger) {
log = logger
dd_trace.UseLogger(NewDataDogLoggerBridge(log))
}
func newLoggerFromEnv() (zerolog.Logger, error) {
var (
level zerolog.Level = DefaultLogLevel
out = os.Stderr
rerr error = nil
)
if v := os.Getenv("BLACKFIRE_LOG_LEVEL"); v != "" {
d, err := strconv.Atoi(v)
if err != nil {
rerr = fmt.Errorf("invalid log level value.(%s)", v)
} else {
level = logLevel(d)
}
}
if v := os.Getenv("BLACKFIRE_LOG_FILE"); v != "" && v != "stderr" {
w, err := os.OpenFile(v, os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
rerr = fmt.Errorf("could not open log file at %s: %v", v, err)
} else {
out = w
}
}
logRecorder.Reset()
logger := zerolog.New(out).Level(level).With().Timestamp().Caller().Logger()
zerolog.TimeFieldFormat = time.RFC3339Nano
return logger, rerr
}
type logrecorder struct {
mu sync.Mutex
logs []string
}
func (l *logrecorder) Output() string {
return strings.Join(l.logs, "\n")
}
func (l *logrecorder) Add(msg string) {
l.mu.Lock()
defer l.mu.Unlock()
l.logs = append(l.logs, msg)
}
func (l *logrecorder) Reset() {
l.mu.Lock()
defer l.mu.Unlock()
l.logs = l.logs[:0] // keep cap.
}
func (l *logrecorder) Contains(s []string) bool {
l.mu.Lock()
defer l.mu.Unlock()
if len(s) == 0 {
return false
}
si := 0
for _, log := range l.logs {
if strings.Contains(strings.ToLower(log), strings.ToLower(s[si])) {
si++
if si == len(s) {
return true
}
}
}
return false
}
type DataDogLoggerBridge struct {
logger zerolog.Logger
}
func NewDataDogLoggerBridge(logger zerolog.Logger) *DataDogLoggerBridge {
return &DataDogLoggerBridge{
logger: logger,
}
}
func (l *DataDogLoggerBridge) Log(msg string) {
level, errorMsg := parseDDLog(msg)
if level == zerolog.NoLevel {
return
}
l.logger.WithLevel(level).Msg(errorMsg)
}
// Used to match a DataDog log, which looks like "Datadog Tracer v1.55.0 ERROR: Uploading profile failed"
var ddLogRegexp = regexp.MustCompile("^Datadog Tracer [0-9v.]+ (DEBUG|INFO|WARN|ERROR): (.*)")
func parseDDLog(msg string) (zerolog.Level, string) {
matches := ddLogRegexp.FindStringSubmatch(msg)
if matches != nil {
switch matches[1] {
case "DEBUG":
return zerolog.DebugLevel, matches[2]
case "INFO":
return zerolog.InfoLevel, matches[2]
case "WARN":
return zerolog.WarnLevel, matches[2]
case "ERROR":
return zerolog.ErrorLevel, matches[2]
}
}
return zerolog.NoLevel, ""
}