/
newrelic.go
183 lines (160 loc) · 3.63 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Package newrelic is a wrapper of the original "github.com/newrelic/go-agent/v3/newrelic".
package newrelic
import (
"encoding/json"
"fmt"
"os"
"reflect"
"time"
"github.com/newrelic/go-agent/v3/newrelic"
)
// LogLevel is level of log that will be printed.
// Will print level that is higher than your
// chosen one.
type LogLevel int8
// Available log levec.
const (
TraceLevel LogLevel = iota - 1
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
PanicLevel
Disabled
)
// Client is newrelic client.
type Client struct {
client *newrelic.Application
level LogLevel
}
// Config is newrelic config.
type Config struct {
Name string
LicenseKey string
Level LogLevel
}
// New to create new newrelic client.
func New(cfg Config) (*Client, error) {
nr, err := newrelic.NewApplication(
newrelic.ConfigAppName(cfg.Name),
newrelic.ConfigLicense(cfg.LicenseKey),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if err != nil {
return nil, err
}
// Wait connection.
if err := nr.WaitForConnection(10 * time.Second); err != nil {
return nil, err
}
return &Client{
client: nr,
level: cfg.Level,
}, nil
}
// NewFromNewrelic to create new newrelic client with
// existing newrelic app.
//
// Don't forget to use `newrelic.ConfigAppLogForwardingEnabled(true)` when
// initializing your newrelic.
func NewFromNewrelicApp(app *newrelic.Application, level LogLevel) *Client {
return &Client{
client: app,
level: level,
}
}
// Trace to print trace log.
func (c *Client) Trace(str string, args ...interface{}) {
if c.level <= TraceLevel {
c.log(TraceLevel, str, args...)
}
}
// Debug to print debug log.
func (c *Client) Debug(str string, args ...interface{}) {
if c.level <= DebugLevel {
c.log(DebugLevel, str, args...)
}
}
// Info to print info log.
func (c *Client) Info(str string, args ...interface{}) {
if c.level <= InfoLevel {
c.log(InfoLevel, str, args...)
}
}
// Warn to print warn log.
func (c *Client) Warn(str string, args ...interface{}) {
if c.level <= WarnLevel {
c.log(WarnLevel, str, args...)
}
}
// Error to print error log.
func (c *Client) Error(str string, args ...interface{}) {
if c.level <= ErrorLevel {
c.log(ErrorLevel, str, args...)
}
}
// Fatal to print fatal log.
// Will exit the program when called.
func (c *Client) Fatal(str string, args ...interface{}) {
if c.level <= FatalLevel {
c.log(FatalLevel, str, args...)
os.Exit(1)
}
}
// Panic to print panic log.
// Will print panic error stack and exit
// like panic().
func (c *Client) Panic(str string, args ...interface{}) {
if c.level <= PanicLevel {
c.log(PanicLevel, str, args...)
panic(fmt.Sprintf(str, args...))
}
}
func (c *Client) log(level LogLevel, str string, args ...interface{}) {
c.Log(map[string]interface{}{
"level": level,
"message": fmt.Sprintf(str, args...),
})
}
// Log to print general log.
// Key `level` can be used to differentiate
// log level.
func (c *Client) Log(fields map[string]interface{}) {
if len(fields) == 0 {
return
}
// Handle level field.
if level, ok := fields["level"]; ok {
lvl := LogLevel(reflect.ValueOf(level).Int())
fields["level"] = c.getLevelStr(lvl)
if c.level > lvl {
return
}
}
data, _ := json.Marshal(fields)
c.client.RecordLog(newrelic.LogData{
Severity: fields["level"].(string),
Message: string(data),
})
}
func (c *Client) getLevelStr(lvl LogLevel) string {
switch lvl {
case TraceLevel:
return "trace"
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warn"
case ErrorLevel:
return "error"
case FatalLevel:
return "fatal"
case PanicLevel:
return "panic"
default:
return "log"
}
}