-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
87 lines (74 loc) · 1.82 KB
/
config.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
package logger
import (
"io"
"os"
"github.com/syllabix/logger/internal/registry"
"github.com/syllabix/logger/mode"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Config has settings that are globally applied to all
// logging instances. It can be configured via a call to Configure
// with a list of options
type Config struct {
mode mode.Kind
// console/local sink
csink io.Writer
// json sink
jsink io.Writer
appname string
level zapcore.Level
}
// sane defaults
var global = &Config{
mode: mode.Development,
csink: os.Stdout,
jsink: nil,
appname: "",
level: zap.InfoLevel,
}
// An Option can be used to apply a value to a setting
// on the global config
type Option func(config *Config)
// AppName sets the "application" field to the provided value
// on all logging contexts
func AppName(name string) Option {
return func(config *Config) {
config.appname = name
}
}
// ConsoleWriter sets the writer that will receive console formatted output
// from a logger
func ConsoleWriter(w io.Writer) Option {
return func(config *Config) {
config.csink = w
}
}
// JSONWriter sets the writer that will receive json formatted output
// from a logger
func JSONWriter(w io.Writer) Option {
return func(config *Config) {
config.jsink = w
}
}
// Mode sets the kind of mode loggers and their respective encoders
// should run in
func Mode(m mode.Kind) Option {
return func(config *Config) {
config.mode = m
}
}
// Level sets the default log level of all logger instances
func Level(lvl zapcore.Level) Option {
return func(config *Config) {
config.level = lvl
}
}
// Configure will apply all the supplied options to a global configuration
// that will be applied to all logger instances.
func Configure(options ...Option) {
for _, opt := range options {
opt(global)
}
registry.SetDefaultLevel(global.level)
}