-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathlog.go
55 lines (42 loc) · 1.22 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
package cmd
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
)
// parseLogLevel parses a given log level and returns a standardized level.
func parseLogLevel(level string) (logrus.Level, error) {
return logrus.ParseLevel(level)
}
// parseLogFormat parses a log format and returns a formatter.
func parseLogFormat(format string) (logrus.Formatter, error) {
switch format {
case "json":
return &logrus.JSONFormatter{}, nil
case "common":
return &logrus.TextFormatter{DisableColors: false, FullTimestamp: true, DisableSorting: true}, nil
default:
return nil, fmt.Errorf("invalid logging format: %s", format)
}
}
// NewLogger returns a new field logger with the provided format, level, and debug configurations.
func NewLogger(format, level string, debug bool) (logrus.FieldLogger, error) {
log := logrus.New()
log.SetOutput(os.Stdout)
logLevelStr := level
if debug {
logLevelStr = "debug"
log.Warnf("Debug flag is deprecated, please consider using --loglevel=DEBUG instead")
}
logLevel, err := parseLogLevel(logLevelStr)
if err != nil {
return log, err
}
log.SetLevel(logLevel)
logFormat, err := parseLogFormat(format)
if err != nil {
return log, err
}
log.SetFormatter(logFormat)
return log, nil
}