-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
77 lines (65 loc) · 1.49 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package conflogger
import (
"github.com/go-courier/logr"
"github.com/sirupsen/logrus"
"os"
"runtime"
"strconv"
"strings"
)
var l = logr.DebugLevel
func SetLevel(lvl logr.Level) {
l = lvl
}
type Log struct {
ReportCaller bool
Level string `env:""`
Output OutputType `env:""`
Format FormatType
init bool
}
func (l *Log) SetDefaults() {
if l.Output == "" {
l.Output = OutputAlways
}
if l.Format == "" {
l.Format = FormatJSON
}
}
func (l *Log) Init() {
if !l.init {
if l.Format == "json" {
logrus.SetFormatter(&logrus.JSONFormatter{
CallerPrettyfier: CallerPrettyfier,
})
} else {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
CallerPrettyfier: CallerPrettyfier,
})
}
logursLevel, logrLevel := getLogLevel(l.Level)
SetLevel(logrLevel)
logrus.SetLevel(logursLevel)
logrus.SetReportCaller(l.ReportCaller)
logrus.SetOutput(os.Stdout)
if err := InstallNewPipeline(l.Output, l.Format); err != nil {
panic(err)
}
l.init = true
}
}
func getLogLevel(l string) (logrus.Level, logr.Level) {
level, err := logrus.ParseLevel(strings.ToLower(l))
if err != nil {
return logrus.DebugLevel, logr.DebugLevel
}
logrLevel, logrLevelErr := logr.ParseLevel(l)
if logrLevelErr != nil {
return logrus.DebugLevel, logr.DebugLevel
}
return level, logrLevel
}
func CallerPrettyfier(f *runtime.Frame) (function string, file string) {
return f.Function + " line:" + strconv.FormatInt(int64(f.Line), 10), ""
}