-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
111 lines (92 loc) · 2.35 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
From JumpServer KoKo
*/
package sidecar
import (
"os"
"strings"
"time"
"github.com/sirupsen/logrus"
)
var logger = logrus.New()
var logLevels = map[string]logrus.Level{
"DEBUG": logrus.DebugLevel,
"INFO": logrus.InfoLevel,
"WARN": logrus.WarnLevel,
"ERROR": logrus.ErrorLevel,
}
/*
Using https://github.com/t-tomalak/logrus-easy-formatter/ as formatter
*/
const (
// Default log format will output [INFO]: 2006-01-02T15:04:05Z07:00 - Log message
defaultLogFormat = "[%lvl%]: %time% - %msg%"
defaultTimestampFormat = time.RFC3339
)
// Formatter implements logrus.Formatter interface.
type Formatter struct {
// Timestamp format
TimestampFormat string
// Available standard keys: time, msg, lvl
// Also can include custom fields but limited to strings.
// All of fields need to be wrapped inside %% i.e %time% %msg%
LogFormat string
// Disables the truncation of the level text to 4 characters.
DisableLevelTruncation bool
}
// Format building log message.
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
output := f.LogFormat
if output == "" {
output = defaultLogFormat
}
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = defaultTimestampFormat
}
output = strings.Replace(output, "%time%", entry.Time.Format(timestampFormat), 1)
output = strings.Replace(output, "%msg%", entry.Message, 1)
level := strings.ToUpper(entry.Level.String())
if !f.DisableLevelTruncation {
level = level[:4]
}
output = strings.Replace(output, "%lvl%", level, 1)
for k, v := range entry.Data {
if s, ok := v.(string); ok {
output = strings.Replace(output, "%"+k+"%", s, 1)
}
}
output += "\n"
return []byte(output), nil
}
func Initial(LogLevel string, fd *os.File) {
level, ok := logLevels[strings.ToUpper(LogLevel)]
if !ok {
level = logrus.InfoLevel
}
formatter := &Formatter{
LogFormat: "%time% [%lvl%] %msg%",
TimestampFormat: "2006-01-02 15:04:05",
}
logger.SetFormatter(formatter)
logger.SetOutput(fd)
logger.SetLevel(level)
}
func Debug(args ...interface{}) {
logger.Debug(args...)
}
func Info(args ...interface{}) {
logger.Info(args...)
}
func Warn(args ...interface{}) {
logger.Warn(args...)
}
func Error(args ...interface{}) {
logger.Error(args...)
}
func Panic(args ...interface{}) {
logrus.Panic(args...)
}
func Fatal(args ...interface{}) {
logrus.Fatal(args...)
}