-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
72 lines (58 loc) · 1.34 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
package logrusconfigurator
import (
"github.com/pkg/errors"
"github.com/psyb0t/gonfiguration"
"github.com/sirupsen/logrus"
)
const (
configKeyLogLevel = "LOG_LEVEL"
configKeyLogFormat = "LOG_FORMAT"
configKeyLogCaller = "LOG_CALLER"
)
const (
defaultReportCaller = false
defaultLevel = levelInfo
defaultFormat = formatText
)
type config struct {
Level level `env:"LOG_LEVEL"`
Format format `env:"LOG_FORMAT"`
ReportCaller bool `env:"LOG_CALLER"`
}
func (c config) log() {
logrus.Debugf(
"logrus-configurator: level: %s, format: %s, reportCaller: %t",
c.Level,
c.Format,
c.ReportCaller,
)
}
//nolint:gochecknoinits
func init() {
if err := configure(); err != nil {
logrus.Panic(err)
}
}
func configure() error {
setDefaults()
c := config{}
if err := gonfiguration.Parse(&c); err != nil {
return errors.Wrap(err, "failed to parse log config")
}
if err := setLevel(c.Level); err != nil {
return errors.Wrap(err, "failed to set log level")
}
if err := setFormat(c.Format); err != nil {
return errors.Wrap(err, "failed to set log format")
}
logrus.SetReportCaller(c.ReportCaller)
c.log()
return nil
}
func setDefaults() {
gonfiguration.SetDefaults(map[string]interface{}{
configKeyLogLevel: defaultLevel,
configKeyLogFormat: defaultFormat,
configKeyLogCaller: defaultReportCaller,
})
}