-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.go
60 lines (49 loc) · 1.25 KB
/
setup.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
package logrusutil
import (
"errors"
"io/ioutil"
"sync"
"github.com/sirupsen/logrus"
)
var (
// ErrLevelNotProvided is returned when a level was not provided
// in the config struct.
ErrLevelNotProvided = errors.New("level not provided")
mtx = &sync.Mutex{}
)
// ConfigureLogger will use the provided configuration to setup the root
// logrus logger.
func ConfigureLogger(logger *logrus.Logger, config *Config) error {
// Not all of the below operations are thread safe in logrus.
mtx.Lock()
defer mtx.Unlock()
if config.Level == "" || config.HookLevel == "" {
return ErrLevelNotProvided
}
if config.Level == DisabledLevel {
logger.Out = ioutil.Discard
// We set the level to panic here because even though we're
// discarding output logrus will still log.
logger.SetLevel(logrus.PanicLevel)
}
if config.Level != DisabledLevel {
// Setup the logger's root level.
level, err := logrus.ParseLevel(config.Level)
if err != nil {
return err
}
logger.SetLevel(level)
}
if config.HookLevel != DisabledLevel {
level, err := logrus.ParseLevel(config.HookLevel)
if err != nil {
return err
}
logger.Hooks.Add(
NewCallerHook(
false, config.HookStackLevel,
config.CallerHookField, level))
return nil
}
return nil
}