-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlog.go
41 lines (35 loc) · 793 Bytes
/
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
package log
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var Logger = logrus.New()
func SetupLogger(cmd *cobra.Command) error {
level, err := cmd.Flags().GetString("log-level")
if err != nil {
return fmt.Errorf("unrecognized log-level: %w", err)
}
var logLevel logrus.Level
switch l := strings.ToLower(level); l {
case "trace":
logLevel = logrus.TraceLevel
case "debug":
logLevel = logrus.DebugLevel
case "info":
logLevel = logrus.InfoLevel
case "warn":
logLevel = logrus.WarnLevel
case "error":
logLevel = logrus.ErrorLevel
case "fatal":
logLevel = logrus.FatalLevel
case "panic":
logLevel = logrus.PanicLevel
default:
return fmt.Errorf("unrecognized log-level: %s.", l)
}
Logger.SetLevel(logLevel)
return nil
}