-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
154 lines (132 loc) · 4.39 KB
/
log.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
// Package log is a wrapper around the zap logger, providing easier integration
// of logging into applications and packages.
package log
import (
"sync"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
_mx sync.Mutex
_once sync.Once
_logger *zap.Logger
)
// Register registers a Logger globally. Register can be called multiple times,
// but only the first call will register a logger, all subsequent calls are
// a no-op.
func Register(l *Logger) error {
_mx.Lock()
defer _mx.Unlock()
p, err := l.Logger()
if err != nil {
return errors.Wrap(err, "log: failed to get zap logger")
}
_once.Do(func() {
_logger = p
zap.RedirectStdLog(p)
})
return nil
}
// Logger likes logs.
type Logger struct {
config *Config
}
// New returns a new configured Logger.
func New(c *Config) (*Logger, error) {
l := &Logger{
config: c,
}
if l.config.Encoder == nil {
return nil, errors.New("log: no encoder configured")
}
if l.config.Output == nil {
return nil, errors.New("log: no output configured")
}
return l, nil
}
// Logger returns a zap Logger that can be used to actually log things.
func (l *Logger) Logger() (*zap.Logger, error) {
return zap.New(
zapcore.NewCore(
l.config.Encoder,
zapcore.AddSync(l.config.Output),
l.config.Level,
),
// Add the caller field to log output.
zap.AddCaller(),
// Required because logger function calls are routed through this package.
zap.AddCallerSkip(1),
// Only log stacktraces on panics or higher, default is for warnings.
zap.AddStacktrace(zap.PanicLevel),
), nil
}
// Log returns the globally configured logger instance.
func Log() *zap.Logger {
return _logger
}
// Check returns a CheckedEntry if logging a message at the specified level
// is enabled. It's a completely optional optimization; in high-performance
// applications, Check can help avoid allocating a slice to hold fields.
func Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
return _logger.Check(lvl, msg)
}
// Core returns the Logger's underlying zapcore.Core.
func Core() zapcore.Core {
return _logger.Core()
}
// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Debug(msg string, fields ...zap.Field) {
_logger.Debug(msg, fields...)
}
// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Error(msg string, fields ...zap.Field) {
_logger.Error(msg, fields...)
}
// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func Fatal(msg string, fields ...zap.Field) {
_logger.Fatal(msg, fields...)
}
// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Info(msg string, fields ...zap.Field) {
_logger.Info(msg, fields...)
}
// Named adds a new path segment to the logger's name. Segments are joined by
// periods. By default, Loggers are unnamed.
func Named(s string) *zap.Logger {
return _logger.Named(s)
}
// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func Panic(msg string, fields ...zap.Field) {
_logger.Panic(msg, fields...)
}
// Sync calls the underlying Core's Sync method, flushing any buffered log
// entries. Applications should take care to call Sync before exiting.
func Sync() {
_ = _logger.Sync()
}
// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Warn(msg string, fields ...zap.Field) {
_logger.Warn(msg, fields...)
}
// With creates a child logger and adds structured context to it. Fields added
// to the child don't affect the parent, and vice versa.
func With(fields ...zap.Field) *zap.Logger {
return _logger.WithOptions(zap.AddCallerSkip(-1)).With(fields...)
}
// WithOptions clones the current Logger, applies the supplied Options, and
// returns the resulting Logger. It's safe to use concurrently.
func WithOptions(opts ...zap.Option) *zap.Logger {
return _logger.WithOptions(opts...)
}