-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
logrus.go
59 lines (50 loc) · 1.31 KB
/
logrus.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
package log
import (
"fmt"
"os"
"strings"
adapter "github.com/bombsimon/logrusr/v2"
"github.com/go-logr/logr"
"github.com/sirupsen/logrus"
"github.com/argoproj/argo-cd/v2/common"
)
const (
JsonFormat = "json"
TextFormat = "text"
)
func NewLogrusLogger(fieldLogger logrus.FieldLogger) logr.Logger {
return adapter.New(fieldLogger, adapter.WithFormatter(func(val interface{}) string {
return fmt.Sprintf("%v", val)
}))
}
// NewWithCurrentConfig create logrus logger by using current configuration
func NewWithCurrentConfig() *logrus.Logger {
l := logrus.New()
l.SetFormatter(CreateFormatter(os.Getenv(common.EnvLogFormat)))
l.SetLevel(createLogLevel())
return l
}
// CreateFormatter create logrus formatter by string
func CreateFormatter(logFormat string) logrus.Formatter {
var formatType logrus.Formatter
switch strings.ToLower(logFormat) {
case JsonFormat:
formatType = &logrus.JSONFormatter{}
case TextFormat:
if os.Getenv("FORCE_LOG_COLORS") == "1" {
formatType = &logrus.TextFormatter{ForceColors: true}
} else {
formatType = &logrus.TextFormatter{}
}
default:
formatType = &logrus.TextFormatter{}
}
return formatType
}
func createLogLevel() logrus.Level {
level, err := logrus.ParseLevel(os.Getenv(common.EnvLogLevel))
if err != nil {
level = logrus.InfoLevel
}
return level
}