-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
logger.go
92 lines (74 loc) · 1.91 KB
/
logger.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package log
//go:generate go run github.com/abice/go-enum -f=$GOFILE --marshal --names
import (
"io"
"strings"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)
// Logger is the global logging instance
//
//nolint:gochecknoglobals
var logger *logrus.Logger
// FormatType format for logging ENUM(
// text // logging as text
// json // JSON format
// )
type FormatType int
// Level log level ENUM(
// info
// trace
// debug
// warn
// error
// fatal
// )
type Level int
//nolint:gochecknoinits
func init() {
logger = logrus.New()
ConfigureLogger(LevelInfo, FormatTypeText, true)
}
// Log returns the global logger
func Log() *logrus.Logger {
return logger
}
// PrefixedLog return the global logger with prefix
func PrefixedLog(prefix string) *logrus.Entry {
return logger.WithField("prefix", prefix)
}
// EscapeInput removes line breaks from input
func EscapeInput(input string) string {
result := strings.ReplaceAll(input, "\n", "")
result = strings.ReplaceAll(result, "\r", "")
return result
}
// ConfigureLogger applies configuration to the global logger
func ConfigureLogger(logLevel Level, formatType FormatType, logTimestamp bool) {
if level, err := logrus.ParseLevel(logLevel.String()); err != nil {
logger.Fatalf("invalid log level %s %v", logLevel, err)
} else {
logger.SetLevel(level)
}
switch formatType {
case FormatTypeText:
logFormatter := &prefixed.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
ForceFormatting: true,
ForceColors: false,
QuoteEmptyFields: true,
DisableTimestamp: !logTimestamp}
logFormatter.SetColorScheme(&prefixed.ColorScheme{
PrefixStyle: "blue+b",
TimestampStyle: "white+h",
})
logger.SetFormatter(logFormatter)
case FormatTypeJson:
logger.SetFormatter(&logrus.JSONFormatter{})
}
}
// Silence disables the logger output
func Silence() {
logger.Out = io.Discard
}