-
Notifications
You must be signed in to change notification settings - Fork 154
/
wrappers.go
136 lines (114 loc) · 3.63 KB
/
wrappers.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
// Copyright 2018 ETH Zurich
// Copyright 2020 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Debug logs at debug level.
func Debug(msg string, ctx ...interface{}) {
if enabled(DebugLevel) {
zap.L().Debug(msg, convertCtx(ctx)...)
}
}
// Info logs at info level.
func Info(msg string, ctx ...interface{}) {
if enabled(InfoLevel) {
zap.L().Info(msg, convertCtx(ctx)...)
}
}
// Error logs at error level.
func Error(msg string, ctx ...interface{}) {
if enabled(ErrorLevel) {
zap.L().Error(msg, convertCtx(ctx)...)
}
}
func enabled(lvl Level) bool {
return zap.L().Core().Enabled(zapcore.Level(lvl))
}
// WithOptions returns the logger with the options applied.
func WithOptions(opts ...Option) Logger {
co := applyOptions(opts)
return &logger{logger: zap.L().WithOptions(co.zapOptions()...)}
}
type Level zapcore.Level
const (
DebugLevel Level = Level(zapcore.DebugLevel)
InfoLevel Level = Level(zapcore.InfoLevel)
ErrorLevel Level = Level(zapcore.ErrorLevel)
)
// Logger describes the logger interface.
type Logger interface {
New(ctx ...interface{}) Logger
Debug(msg string, ctx ...interface{})
Info(msg string, ctx ...interface{})
Error(msg string, ctx ...interface{})
Enabled(lvl Level) bool
}
type logger struct {
logger *zap.Logger
}
// New creates a logger with the given context.
func New(ctx ...interface{}) Logger {
return &logger{logger: zap.L().With(convertCtx(ctx)...)}
}
func (l *logger) New(ctx ...interface{}) Logger {
return &logger{logger: l.logger.With(convertCtx(ctx)...)}
}
func (l *logger) Debug(msg string, ctx ...interface{}) {
if l.Enabled(DebugLevel) {
l.logger.Debug(msg, convertCtx(ctx)...)
}
}
func (l *logger) Info(msg string, ctx ...interface{}) {
if l.Enabled(InfoLevel) {
l.logger.Info(msg, convertCtx(ctx)...)
}
}
func (l *logger) Error(msg string, ctx ...interface{}) {
if l.Enabled(ErrorLevel) {
l.logger.Error(msg, convertCtx(ctx)...)
}
}
func (l *logger) Enabled(lvl Level) bool {
return l.logger.Core().Enabled(zapcore.Level(lvl))
}
// Root returns the root logger. It's a logger without any context.
func Root() Logger {
return &logger{logger: zap.L()}
}
// Discard sets the logger up to discard all log entries. This is useful for
// testing.
func Discard() {
Root().(*logger).logger = zap.NewNop()
}
// DiscardLogger implements the Logger interface and discards all messages.
// Subloggers created from this logger will also discard all messages and
// ignore the additional context.
//
// To see how to use this, see the example.
type DiscardLogger struct{}
func (d DiscardLogger) New(ctx ...interface{}) Logger { return d }
func (DiscardLogger) Debug(msg string, ctx ...interface{}) {}
func (DiscardLogger) Info(msg string, ctx ...interface{}) {}
func (DiscardLogger) Error(msg string, ctx ...interface{}) {}
func (DiscardLogger) Enabled(lvl Level) bool { return false }
func convertCtx(ctx []interface{}) []zap.Field {
fields := make([]zap.Field, 0, len(ctx)/2)
for i := 0; i+1 < len(ctx); i += 2 {
fields = append(fields, zap.Any(ctx[i].(string), ctx[i+1]))
}
return fields
}