forked from smartcontractkit/chainlink
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
267 lines (234 loc) · 6.04 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Package logger is used to store details of events in the node.
// Events can be categorized by Debug, Info, Error, Fatal, and Panic.
package logger
import (
stderr "errors"
"fmt"
"log"
"net/url"
"os"
"reflect"
"runtime"
"sync"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
logger *Logger
mtx sync.RWMutex
)
func init() {
err := zap.RegisterSink("pretty", prettyConsoleSink(os.Stderr))
if err != nil {
log.Fatalf("failed to register pretty printer %+v", err)
}
err = registerOSSinks()
if err != nil {
log.Fatalf("failed to register os specific sinks %+v", err)
}
zl, err := zap.NewProduction()
if err != nil {
log.Fatal(err)
}
SetLogger(zl)
}
func GetLogger() *Logger {
mtx.RLock()
defer mtx.RUnlock()
return logger
}
func prettyConsoleSink(s zap.Sink) func(*url.URL) (zap.Sink, error) {
return func(*url.URL) (zap.Sink, error) {
return PrettyConsole{s}, nil
}
}
// Logger holds a field for the logger interface.
type Logger struct {
*zap.SugaredLogger
}
// Write logs a message at the Info level and returns the length
// of the given bytes.
func (l *Logger) Write(b []byte) (int, error) {
l.Info(string(b))
return len(b), nil
}
// SetLogger sets the internal logger to the given input.
func SetLogger(zl *zap.Logger) {
mtx.Lock()
defer mtx.Unlock()
if logger != nil {
defer func() {
if err := logger.Sync(); err != nil {
if stderr.Unwrap(err).Error() != os.ErrInvalid.Error() &&
stderr.Unwrap(err).Error() != "inappropriate ioctl for device" &&
stderr.Unwrap(err).Error() != "bad file descriptor" {
// logger.Sync() will return 'invalid argument' error when closing file
log.Fatalf("failed to sync logger %+v", err)
}
}
}()
}
logger = &Logger{zl.Sugar()}
}
// CreateProductionLogger returns a log config for the passed directory
// with the given LogLevel and customizes stdout for pretty printing.
func CreateProductionLogger(
dir string, jsonConsole bool, lvl zapcore.Level, toDisk bool) *zap.Logger {
config := zap.NewProductionConfig()
if !jsonConsole {
config.OutputPaths = []string{"pretty://console"}
}
if toDisk {
destination := logFileURI(dir)
config.OutputPaths = append(config.OutputPaths, destination)
config.ErrorOutputPaths = append(config.ErrorOutputPaths, destination)
}
config.Level.SetLevel(lvl)
zl, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
log.Fatal(err)
}
return zl
}
// Infow logs an info message and any additional given information.
func Infow(msg string, keysAndValues ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Infow(msg, keysAndValues...)
}
// Debugw logs a debug message and any additional given information.
func Debugw(msg string, keysAndValues ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Debugw(msg, keysAndValues...)
}
// Warnw logs a debug message and any additional given information.
func Warnw(msg string, keysAndValues ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Warnw(msg, keysAndValues...)
}
// Errorw logs an error message, any additional given information, and includes
// stack trace.
func Errorw(msg string, keysAndValues ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Errorw(msg, keysAndValues...)
}
// Infof formats and then logs the message.
func Infof(format string, values ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Info(fmt.Sprintf(format, values...))
}
// Debugf formats and then logs the message.
func Debugf(format string, values ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Debug(fmt.Sprintf(format, values...))
}
// Warnf formats and then logs the message as Warn.
func Warnf(format string, values ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Warn(fmt.Sprintf(format, values...))
}
// Panicf formats and then logs the message before panicking.
func Panicf(format string, values ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Panic(fmt.Sprintf(format, values...))
}
// Info logs an info message.
func Info(args ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Info(args...)
}
// Debug logs a debug message.
func Debug(args ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Debug(args...)
}
// Warn logs a message at the warn level.
func Warn(args ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Warn(args...)
}
// Error logs an error message.
func Error(args ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Error(args...)
}
// WarnIf logs the error if present.
func WarnIf(err error) {
if err != nil {
mtx.RLock()
defer mtx.RUnlock()
logger.Warn(err)
}
}
// ErrorIf logs the error if present.
func ErrorIf(err error, optionalMsg ...string) {
if err != nil {
mtx.RLock()
defer mtx.RUnlock()
if len(optionalMsg) > 0 {
logger.Error(errors.Wrap(err, optionalMsg[0]))
} else {
logger.Error(err)
}
}
}
// ErrorIfCalling calls the given function and logs the error of it if there is.
func ErrorIfCalling(f func() error, optionalMsg ...string) {
err := f()
if err != nil {
mtx.RLock()
defer mtx.RUnlock()
e := errors.Wrap(err, runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name())
if len(optionalMsg) > 0 {
logger.Error(errors.Wrap(e, optionalMsg[0]))
} else {
logger.Error(e)
}
}
}
// PanicIf logs the error if present.
func PanicIf(err error) {
if err != nil {
mtx.RLock()
defer mtx.RUnlock()
logger.Panic(err)
}
}
// Fatal logs a fatal message then exits the application.
func Fatal(args ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Fatal(args...)
}
// Errorf logs a message at the error level using Sprintf.
func Errorf(format string, values ...interface{}) {
Error(fmt.Sprintf(format, values...))
}
// Fatalf logs a message at the fatal level using Sprintf.
func Fatalf(format string, values ...interface{}) {
Fatal(fmt.Sprintf(format, values...))
}
// Panic logs a panic message then panics.
func Panic(args ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Panic(args...)
}
// Sync flushes any buffered log entries.
func Sync() error {
mtx.RLock()
defer mtx.RUnlock()
return logger.Sync()
}