forked from smartcontractkit/chainlink
/
logger.go
130 lines (108 loc) · 3.03 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
// 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 (
"fmt"
"log"
"path"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *Logger
func init() {
zl, err := zap.NewProduction()
if err != nil {
log.Fatal(err)
}
SetLogger(NewLogger(zl))
}
// 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) (n int, err error) {
l.Info(string(b))
return len(b), nil
}
// NewLogger returns the logger updated with the given Logger.
func NewLogger(zl *zap.Logger) *Logger {
return &Logger{zl.Sugar()}
}
// SetLogger sets the internal logger to the given input.
func SetLogger(l *Logger) {
if logger != nil {
defer logger.Sync()
}
logger = l
}
// Reconfigure creates a new log file at the configured directory
// with the given LogLevel.
func Reconfigure(dir string, lvl zapcore.Level) {
config := generateConfig(dir)
config.Level.SetLevel(lvl)
zl, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
log.Fatal(err)
}
SetLogger(NewLogger(zl))
}
func generateConfig(dir string) zap.Config {
config := zap.NewProductionConfig()
destination := path.Join(dir, "log.jsonl")
config.OutputPaths = []string{"stderr", destination}
config.ErrorOutputPaths = []string{"stderr", destination}
return config
}
// Infow logs an info message and any additional given information.
func Infow(msg string, keysAndValues ...interface{}) {
logger.Infow(msg, keysAndValues...)
}
// Debugw logs a debug message and any additional given information.
func Debugw(msg string, keysAndValues ...interface{}) {
logger.Debugw(msg, keysAndValues...)
}
// Warnw logs a debug message and any additional given information.
func Warnw(msg string, keysAndValues ...interface{}) {
logger.Warnw(msg, keysAndValues...)
}
// Errorw logs an error message, any additional given information, and includes
// stack trace.
func Errorw(msg string, keysAndValues ...interface{}) {
logger.Errorw(msg, keysAndValues...)
}
// Panicf formats and then logs the message before panicking.
func Panicf(format string, values ...interface{}) {
logger.Panic(fmt.Sprintf(format, values...))
}
// Info logs an info message using Sprint.
func Info(args ...interface{}) {
logger.Info(args...)
}
// Warn logs a message at the warn level using Sprint.
func Warn(args ...interface{}) {
logger.Warn(args...)
}
// Error logs an error message using Sprint.
func Error(args ...interface{}) {
logger.Error(args...)
}
//WarnIf logs the error if present.
func WarnIf(err error) {
if err != nil {
logger.Warn(err)
}
}
// Fatal logs a fatal message then exits the application using Sprint.
func Fatal(args ...interface{}) {
logger.Fatal(args...)
}
// Panic logs a panic message then panics using Sprint.
func Panic(args ...interface{}) {
logger.Panic(args...)
}
// Sync flushes any buffered log entries.
func Sync() error {
return logger.Sync()
}