-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogging.go
85 lines (75 loc) · 2.07 KB
/
logging.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
package metrics
import (
"bytes"
"encoding/json"
"fmt"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/sirupsen/logrus"
)
// CustomNewRelicContextLogFormatter is a logrus.Formatter that will format logs for sending
// to New Relic. This is a custom implementation that includes sending all logrus.Entry.Fields,
// which isn't supported out of the box yet
//
// Based off of: https://github.com/newrelic/go-agent/blob/f1942e10f0819e2c854d5d7289eb0dc1c52a00af/v3/integrations/logcontext-v2/nrlogrus/formatter.go
type CustomNewRelicContextLogFormatter struct {
app *newrelic.Application
formatter logrus.Formatter
}
func NewCustomNewRelicLogFormatter(app *newrelic.Application, formatter logrus.Formatter) CustomNewRelicContextLogFormatter {
return CustomNewRelicContextLogFormatter{
app: app,
formatter: formatter,
}
}
func (f CustomNewRelicContextLogFormatter) Format(e *logrus.Entry) ([]byte, error) {
message := e.Message
if len(e.Data) > 0 {
errorString := "<nil>"
extraData := make(map[string]interface{})
for k, v := range e.Data {
if k == "error" {
switch typed := v.(type) {
case error:
errorString = fmt.Sprintf("\"%s\"", typed.Error())
default:
}
} else {
extraData[k] = v
}
}
extraDataJsonBytes, err := json.Marshal(extraData)
if err == nil {
message = fmt.Sprintf("message=\"%s\", error=%s, data=%s", message, errorString, string(extraDataJsonBytes))
}
}
logData := newrelic.LogData{
Severity: e.Level.String(),
Message: message,
}
logBytes, err := f.formatter.Format(e)
if err != nil {
return nil, err
}
logBytes = bytes.TrimRight(logBytes, "\n")
b := bytes.NewBuffer(logBytes)
ctx := e.Context
var txn *newrelic.Transaction
if ctx != nil {
txn = newrelic.FromContext(ctx)
}
if txn != nil {
txn.RecordLog(logData)
err := newrelic.EnrichLog(b, newrelic.FromTxn(txn))
if err != nil {
return nil, err
}
} else {
f.app.RecordLog(logData)
err := newrelic.EnrichLog(b, newrelic.FromApp(f.app))
if err != nil {
return nil, err
}
}
b.WriteString("\n")
return b.Bytes(), nil
}