-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
executable file
·184 lines (164 loc) · 4.38 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"github.com/sirupsen/logrus"
"runtime"
"strings"
)
var logger = logrus.New()
func SetLogLevel(level logrus.Level) {
logger.Level = level
}
func SetLogFormatter(formatter logrus.Formatter) {
logger.Formatter = formatter
}
// Debug logs a message at level Debug on the standard logger.
func Debug(args ...interface{}) {
if logger.Level >= logrus.DebugLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Debug(args...)
}
}
// Debug logs with format message at level Debug on the standard logger.
func Debugf(format string, args ...interface{}) {
if logger.Level >= logrus.DebugLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Debugf(format, args...)
}
}
// Info logs a message at level Info on the standard logger.
func Info(args ...interface{}) {
if logger.Level >= logrus.InfoLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Info(args...)
}
}
// Info logs with format message at level Info on the standard logger.
func Infof(format string, args ...interface{}) {
if logger.Level >= logrus.InfoLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Infof(format, args...)
}
}
// Warn logs a message at level Warn on the standard logger.
func Warn(args ...interface{}) {
if logger.Level >= logrus.WarnLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Warn(args...)
}
}
// Warn logs with format message at level Warn on the standard logger.
func Warnf(format string, args ...interface{}) {
if logger.Level >= logrus.WarnLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Warnf(format, args...)
}
}
// Error logs a message at level Error on the standard logger.
func Error(args ...interface{}) {
if logger.Level >= logrus.ErrorLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Error(args...)
}
}
// Error logs with format message at level Error on the standard logger.
func Errorf(format string, args ...interface{}) {
if logger.Level >= logrus.ErrorLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Errorf(format, args...)
}
}
// Fatal logs a message at level Fatal on the standard logger.
func Fatal(args ...interface{}) {
if logger.Level >= logrus.FatalLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Fatal(args...)
}
}
// Fatal logs with format message at level Fatal on the standard logger.
func Fatalf(format string, args ...interface{}) {
if logger.Level >= logrus.FatalLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Fatalf(format, args...)
}
}
// Panic logs a message at level Panic on the standard logger.
func Panic(args ...interface{}) {
if logger.Level >= logrus.PanicLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Panic(args...)
}
}
// Panic logs with format message at level Panic on the standard logger.
func Panicf(format string, args ...interface{}) {
if logger.Level >= logrus.PanicLevel {
entry := logger.WithFields(logrus.Fields{})
if cmdOptions.Debug {
entry.Data["file"] = fileInfo(2)
}
entry.Panicf(format, args...)
}
}
// Display the file and the line number where the command
// was executed
func fileInfo(skip int) string {
_, file, line, ok := runtime.Caller(skip)
if !ok {
file = "<???>"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
return fmt.Sprintf("%s:%d", file, line)
}
// Setup or Initialize the logger
func initLogger(verbose bool) {
// Set the formatter option for logrus
formatter := new(logrus.TextFormatter)
formatter.TimestampFormat = "2006-01-02 15:04:05"
formatter.FullTimestamp = true
// Set the formatter.
SetLogFormatter(formatter)
// if log level
if verbose {
SetLogLevel(logrus.DebugLevel)
} else {
SetLogLevel(logrus.InfoLevel)
}
}