-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
57 lines (51 loc) · 1.43 KB
/
logging.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
package config
import (
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"io"
"os"
"strings"
)
// LoggingLevel is the verbosity level of logging
type LoggingLevel = string
const (
// Debug is the most verbose logging level and will
// report all logs
Debug = "DEBUG"
// Info level reports logrus.Info, logrus.Warn, and logrus.Error
Info = "INFO"
// Warn level reports logrus.Warn, and logrus.Error
Warn = "WARN"
// Error level reports ony error messages
Error = "ERROR"
// Off level turns off logs from logrus. instead the "stylized"
// logging using terminal spinners will be used
Off = "OFF"
)
// initLogger will initialize the logging configuration of the application
// and set the logging level to the given LoggingLevel. Defaults to Off
func initLogger(level LoggingLevel) {
logrus.SetOutput(os.Stderr)
switch strings.ToUpper(level) {
case Debug:
logrus.SetLevel(logrus.DebugLevel)
case Info:
logrus.SetLevel(logrus.InfoLevel)
case Warn:
logrus.SetLevel(logrus.WarnLevel)
case Error:
logrus.SetLevel(logrus.ErrorLevel)
case Off:
fallthrough
default:
logrus.SetOutput(io.Discard)
}
}
// ShouldUseSpinner will return true if the "stylized"
func ShouldUseSpinner() bool {
return !viper.IsSet(ViperLogLevelKey) || strings.ToUpper(viper.GetString(ViperLogLevelKey)) == "OFF"
}
// ShouldUseColor will return true, if colored output should be used
func ShouldUseColor() bool {
return !viper.GetBool("disable_color")
}