-
Notifications
You must be signed in to change notification settings - Fork 1
/
configs.go
76 lines (67 loc) · 2.46 KB
/
configs.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
package logrusiowriter
import "github.com/sirupsen/logrus"
// Config holds the configuration to be used with WithConfig() configurer
// This struct is useful to embed into configuration structs parsed with libraries like envconfig
type Config struct {
Level string `default:"info"`
Fields logrus.Fields `default:"logger:stdlib"`
TrailingNewLineTrimming bool `default:"true"`
}
// WithTrailingNewLineTrimming configures trailing newline trimming. This is true by default, because
// log.Print adds a newline in the end of its messages, which does not make sense inside of a logrus log
func WithTrailingNewLineTrimming(trim bool) Configurer {
return func(w *writer) {
w.trailingNewLineTrimming = trim
}
}
// WithLogger configures the logger with the one provided
func WithLogger(logger logrus.FieldLogger) Configurer {
return func(w *writer) {
w.logger = logger
}
}
// WithLevel configures the level with the one provided
func WithLevel(lvl logrus.Level) Configurer {
return func(w *writer) {
w.level = lvl
}
}
// WithFields configures the fields with the ones provided
func WithFields(fields logrus.Fields) Configurer {
return func(w *writer) {
w.fields = fields
}
}
// WithConfig creates a configurer from the configuration provided as a struct
// If it's unable to parse the Level provided as a string, it will invoke the OnLevelParseError function and set the
// level returned by that function (a default value)
func WithConfig(cfg Config) Configurer {
return func(w *writer) {
lvl, err := logrus.ParseLevel(cfg.Level)
if err != nil {
lvl = OnLevelParseError(err)
}
w.level = lvl
w.fields = cfg.Fields
}
}
// WithConfigInterface creates a configurer from the configuration provided as an interface
func WithConfigInterface(cfg interface {
Level() logrus.Level
Fields() logrus.Fields
Logger() logrus.FieldLogger
}) Configurer {
return func(w *writer) {
w.logger = cfg.Logger()
w.level = cfg.Level()
w.fields = cfg.Fields()
}
}
// OnLevelParseError will be invoked if logrus is unable to parse the string level provided in the configuration
// The default behavior is to log it with logrus and return a default Info level,
// you can change this to log in some other system or to panic
// Changing this is not thread safe, so it might be a good idea to change it in a init() function
var OnLevelParseError = func(err error) logrus.Level {
logrus.Errorf("Can't parse level: %s", err)
return logrus.InfoLevel
}