/
logger.go
206 lines (173 loc) · 4.82 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
// 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"
"net/url"
"os"
"github.com/fatih/color"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *Logger
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 {
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) {
if logger != nil {
defer logger.Sync()
}
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
}
// CreateTestLogger creates a logger that directs output to PrettyConsole
// configured for test output.
func CreateTestLogger() *zap.Logger {
color.NoColor = false
config := zap.NewProductionConfig()
config.Level.SetLevel(zapcore.DebugLevel)
config.OutputPaths = []string{"pretty://console"}
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{}) {
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...)
}
// Infof formats and then logs the message.
func Infof(format string, values ...interface{}) {
logger.Info(fmt.Sprintf(format, values...))
}
// Debugf formats and then logs the message.
func Debugf(format string, values ...interface{}) {
logger.Debug(fmt.Sprintf(format, values...))
}
// Warnf formats and then logs the message as Warn.
func Warnf(format string, values ...interface{}) {
logger.Warn(fmt.Sprintf(format, values...))
}
// 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.
func Info(args ...interface{}) {
logger.Info(args...)
}
// Debug logs a debug message.
func Debug(args ...interface{}) {
logger.Debug(args...)
}
// Warn logs a message at the warn level.
func Warn(args ...interface{}) {
logger.Warn(args...)
}
// Error logs an error message.
func Error(args ...interface{}) {
logger.Error(args...)
}
// WarnIf logs the error if present.
func WarnIf(err error) {
if err != nil {
logger.Warn(err)
}
}
// ErrorIf logs the error if present.
func ErrorIf(err error) {
if err != nil {
logger.Error(err)
}
}
// PanicIf logs the error if present.
func PanicIf(err error) {
if err != nil {
logger.Panic(err)
}
}
// Fatal logs a fatal message then exits the application.
func Fatal(args ...interface{}) {
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{}) {
logger.Panic(args...)
}
// Sync flushes any buffered log entries.
func Sync() error {
return logger.Sync()
}