-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
63 lines (52 loc) · 1.35 KB
/
logger.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
package narada
import (
"fmt"
"io/ioutil"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func NewLogger(config *viper.Viper) (*logrus.Logger, error) {
config.SetDefault("logger.formatter", "text")
config.SetDefault("logger.level", "debug")
config.SetDefault("logger.catch_errors", true)
// Setting level
logger := logrus.New()
switch lvl := config.GetString("logger.level"); lvl {
case "debug":
logger.SetLevel(logrus.DebugLevel)
case "info":
logger.SetLevel(logrus.InfoLevel)
case "warn":
logger.SetLevel(logrus.WarnLevel)
case "error":
logger.SetLevel(logrus.ErrorLevel)
default:
return nil, fmt.Errorf("unknown logger level provided: %s", lvl)
}
// Settings formatter
switch format := config.GetString("logger.formatter"); format {
case "text":
logger.SetFormatter(&logrus.TextFormatter{})
case "json":
logger.SetFormatter(&logrus.JSONFormatter{})
default:
return nil, fmt.Errorf("unknown formatter: %s", format)
}
// Catching errors with Sentry
if config.GetBool("logger.catch_errors") {
client, err := NewSentry(config)
if err != nil {
return nil, err
}
hook := NewLogrusSentryHook(client)
logger.AddHook(hook)
}
hook := NewLogrusSlackHook(config)
logger.AddHook(hook)
return logger, nil
}
func NewNopLogger() *logrus.Logger {
logger := logrus.New()
logger.Out = ioutil.Discard
return logger
}