-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
default.go
187 lines (155 loc) · 4.79 KB
/
default.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
package logger
import (
"errors"
"fmt"
"log"
"os"
"go.uber.org/zap"
)
var (
// Default logger for use throughout the project.
// All the package-level functions are calling Default.
Default *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(&Logger{
SugaredLogger: zl.Sugar(),
})
}
// SetLogger sets the internal logger to the given input.
//
// DEPRECATED this method is deprecated because it leads to race conditions.
// Instead, you should fork the logger.Default instance to create a new logger
// for your module.
// Eg: logger.Default.Named("<my-package-name>")
func SetLogger(newLogger *Logger) {
if Default != nil {
defer func() {
if err := Default.Sync(); err != nil {
if errors.Unwrap(err).Error() != os.ErrInvalid.Error() &&
errors.Unwrap(err).Error() != "inappropriate ioctl for device" &&
errors.Unwrap(err).Error() != "bad file descriptor" {
// logger.Sync() will return 'invalid argument' error when closing file
log.Fatalf("failed to sync logger %+v", err)
}
}
}()
}
Default = newLogger
}
// Infow logs an info message and any additional given information.
func Infow(msg string, keysAndValues ...interface{}) {
Default.Infow(msg, keysAndValues...)
}
// Debugw logs a debug message and any additional given information.
func Debugw(msg string, keysAndValues ...interface{}) {
Default.Debugw(msg, keysAndValues...)
}
// Tracew is a shim stand-in for when we have real trace-level logging support
func Tracew(msg string, keysAndValues ...interface{}) {
// Zap does not support trace logging just yet
Default.Debugw("TRACE: "+msg, keysAndValues...)
}
// Warnw logs a debug message and any additional given information.
func Warnw(msg string, keysAndValues ...interface{}) {
Default.Warnw(msg, keysAndValues...)
}
// Errorw logs an error message, any additional given information, and includes
// stack trace.
func Errorw(msg string, keysAndValues ...interface{}) {
Default.Errorw(msg, keysAndValues...)
}
// Logs and returns a new error
func NewErrorw(msg string, keysAndValues ...interface{}) error {
Default.Errorw(msg, keysAndValues...)
return errors.New(msg)
}
// Infof formats and then logs the message.
func Infof(format string, values ...interface{}) {
Default.Info(fmt.Sprintf(format, values...))
}
// Debugf formats and then logs the message.
func Debugf(format string, values ...interface{}) {
Default.Debug(fmt.Sprintf(format, values...))
}
// Tracef is a shim stand-in for when we have real trace-level logging support
func Tracef(format string, values ...interface{}) {
Default.Debug("TRACE: " + fmt.Sprintf(format, values...))
}
// Warnf formats and then logs the message as Warn.
func Warnf(format string, values ...interface{}) {
Default.Warn(fmt.Sprintf(format, values...))
}
// Panicf formats and then logs the message before panicking.
func Panicf(format string, values ...interface{}) {
Default.Panic(fmt.Sprintf(format, values...))
}
// Info logs an info message.
func Info(args ...interface{}) {
Default.Info(args...)
}
// Debug logs a debug message.
func Debug(args ...interface{}) {
Default.Debug(args...)
}
// Trace is a shim stand-in for when we have real trace-level logging support
func Trace(args ...interface{}) {
Default.Debug(append([]interface{}{"TRACE: "}, args...))
}
// Warn logs a message at the warn level.
func Warn(args ...interface{}) {
Default.Warn(args...)
}
// Error logs an error message.
func Error(args ...interface{}) {
Default.Error(args...)
}
func WarnIf(err error) {
Default.WarnIf(err)
}
func ErrorIf(err error, optionalMsg ...string) {
Default.ErrorIf(err, optionalMsg...)
}
func ErrorIfCalling(f func() error, optionalMsg ...string) {
Default.ErrorIfCalling(f, optionalMsg...)
}
// Fatal logs a fatal message then exits the application.
func Fatal(args ...interface{}) {
Default.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...))
}
// Fatalw logs a message and exits the application
func Fatalw(msg string, keysAndValues ...interface{}) {
Default.Fatalw(msg, keysAndValues...)
}
// Panic logs a panic message then panics.
func Panic(args ...interface{}) {
Default.Panic(args...)
}
// PanicIf logs the error if present.
func PanicIf(err error) {
Default.PanicIf(err)
}
// Sync flushes any buffered log entries.
func Sync() error {
return Default.Sync()
}