-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.go
198 lines (173 loc) · 5.06 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
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
package flume
import (
"bytes"
"encoding/json"
"fmt"
"github.com/ansel1/merry"
"go.uber.org/zap/zapcore"
"io"
"strconv"
"strings"
)
type (
// Logger is the basic logging interface. Construct instances of Logger with a Factory,
// or with the package functions (which use a package level Factory).
Logger interface {
Debug(msg string, args ...interface{})
Info(msg string, args ...interface{})
Error(msg string, args ...interface{})
IsDebug() bool
IsInfo() bool
// With creates a new Logger with some context already attached. All
// entries logged with the child logger will include this context.
With(args ...interface{}) Logger
}
// Level is a log level
Level zapcore.Level
)
const (
// OffLevel disables all logs
OffLevel = Level(127)
// DebugLevel should be used for low-level, non-production logs. Typically intended only for developers.
DebugLevel = Level(zapcore.DebugLevel)
// InfoLevel should be used for production level logs. Typically intended for end-users and developers.
InfoLevel = Level(zapcore.InfoLevel)
// ErrorLevel should be used for errors. Generally, this should be reserved for events which truly
// need to be looked at by an admin, and might be reported to an error-tracking system.
ErrorLevel = Level(zapcore.ErrorLevel)
)
var pkgFactory = NewFactory()
// New creates a new Logger
func New(name string) Logger {
return pkgFactory.NewLogger(name)
}
// NewCore returns a new Core
func NewCore(name string, options ...CoreOption) *Core {
return pkgFactory.NewCore(name, options...)
}
// ConfigString configures the package level Factory. The
// string can either be a JSON-serialized Config object, or
// just a LevelsString (see Factory.LevelsString for format).
//
// Note: this will reconfigure the logging levels for all
// loggers.
func ConfigString(s string) error {
if strings.HasPrefix(strings.TrimSpace(s), "{") {
// it's json, treat it like a full config string
cfg := Config{}
err := json.Unmarshal([]byte(s), &cfg)
if err != nil {
return err
}
return Configure(cfg)
}
return pkgFactory.LevelsString(s)
}
// Configure configures the package level Factory from
// the settings in the Config object. See Config for
// details.
//
// Note: this will reconfigure the logging levels for all
// loggers.
func Configure(cfg Config) error {
return pkgFactory.Configure(cfg)
}
// SetOut sets the output writer for all logs produced by the default factory.
// Returns a function which sets the output writer back to the prior setting.
func SetOut(w io.Writer) func() {
return pkgFactory.SetOut(w)
}
// SetDefaultLevel sets the default log level on the package-level Factory.
func SetDefaultLevel(l Level) {
pkgFactory.SetDefaultLevel(l)
}
// SetLevel sets a log level for a named logger on the package-level Factory.
func SetLevel(name string, l Level) {
pkgFactory.SetLevel(name, l)
}
// SetAddCaller enables/disables call site logging on the package-level Factory
func SetAddCaller(b bool) {
pkgFactory.SetAddCaller(b)
}
// SetEncoder sets the encoder for the package-level Factory
func SetEncoder(e Encoder) {
pkgFactory.SetEncoder(e)
}
// Hooks adds hooks to the package-level Factory.
func Hooks(hooks ...HookFunc) {
pkgFactory.Hooks(hooks...)
}
// ClearHooks clears all hooks from the package-level Factory.
func ClearHooks() {
pkgFactory.ClearHooks()
}
// SetDevelopmentDefaults sets useful default settings on the package-level Factory
// which are appropriate for a development setting. Default log level is
// set to INF, all loggers are reset to the default level, call site information
// is logged, and the encoder is a colorized, multi-line friendly console
// encoder with a simplified time stamp format.
func SetDevelopmentDefaults() error {
return Configure(Config{
Development: true,
})
}
// String implements stringer and a few other interfaces.
func (l Level) String() string {
switch l {
case DebugLevel:
return "DBG"
case InfoLevel:
return "INF"
case ErrorLevel:
return "ERR"
case OffLevel:
return "OFF"
default:
return fmt.Sprintf("Level(%d)", l)
}
}
// MarshalText implements encoding.TextMarshaler
func (l Level) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler
func (l *Level) UnmarshalText(text []byte) error {
if l == nil {
return merry.New("can't unmarshal a nil *Level")
}
if !l.unmarshalText(text) {
return fmt.Errorf("unrecognized level: %q", text)
}
return nil
}
func (l *Level) unmarshalText(text []byte) bool {
text = bytes.ToLower(text)
switch string(text) {
case "debug", "dbg", "all":
*l = DebugLevel
case "info", "inf", "": // make the zero value useful
*l = InfoLevel
case "error", "err":
*l = ErrorLevel
case "off":
*l = OffLevel
default:
if i, err := strconv.Atoi(string(text)); err != nil {
if i >= -127 && i <= 127 {
*l = Level(i)
} else {
return false
}
}
return false
}
return true
}
// Set implements flags.Value
func (l *Level) Set(s string) error {
return l.UnmarshalText([]byte(s))
}
// Get implements flag.Getter
func (l *Level) Get() interface{} {
return *l
}