-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
243 lines (196 loc) · 5.75 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package log
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/go-chi/chi/middleware"
"github.com/sirupsen/logrus"
)
// Fields Type to pass when we want to call WithFields for structured logging
type Fields map[string]interface{}
// Logger interface
type Logger interface {
Info(args ...interface{})
Infof(message string, args ...interface{})
Debug(args ...interface{})
Debugf(message string, args ...interface{})
Error(args ...interface{})
Errorf(message string, args ...interface{})
Warn(args ...interface{})
Warnf(message string, args ...interface{})
Fatal(args ...interface{})
Fatalf(message string, args ...interface{})
Panic(args ...interface{})
Panicf(message string, args ...interface{})
Writer() *io.PipeWriter
}
var (
logger *logrus.Logger
)
func init() {
New(false, "info")
}
// New - Creates a new instance of logrus with customized configuration
func New(isJSONFormatted bool, logLevel string) *logrus.Logger {
var formatter logrus.Formatter
formatter = &logrus.TextFormatter{
ForceColors: true,
DisableLevelTruncation: true,
}
if isJSONFormatted {
formatter = &logrus.JSONFormatter{}
}
log := logrus.New()
log.SetFormatter(formatter)
log.SetLevel(getLevel(logLevel))
logger = log
return log
}
func GetLogger() Logger {
return logger
}
// RequestLogger creates a logger with the request ID on it
func RequestLogger(ctx context.Context) Logger {
return logger.WithFields(logrus.Fields{
"requestID": middleware.GetReqID(ctx),
})
}
func Writer() *io.PipeWriter {
return logger.Writer()
}
func WriterLevel(logLevel string) *io.PipeWriter {
return logger.WriterLevel(getLevel(logLevel))
}
func getLevel(logLevel string) logrus.Level {
level := logrus.InfoLevel
switch logLevel {
case "panic":
level = logrus.PanicLevel
case "fatal":
level = logrus.FatalLevel
case "error":
level = logrus.ErrorLevel
case "warn":
level = logrus.WarnLevel
case "info":
level = logrus.InfoLevel
case "debug":
level = logrus.DebugLevel
}
return level
}
func Info(args ...interface{}) {
logger.Info(args...)
}
func Infof(message string, args ...interface{}) {
logger.Infof(message, args...)
}
func InfoWithFields(fields Fields, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Info(args...)
}
func InfoWithFieldsf(fields Fields, message string, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Infof(message, args...)
}
func Debug(args ...interface{}) {
logger.Debug(args...)
}
func Debugf(message string, args ...interface{}) {
logger.Debugf(message, args...)
}
func DebugWithFields(fields Fields, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Debug(args...)
}
func DebugWithFieldsf(fields Fields, message string, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Debugf(message, args...)
}
func Error(args ...interface{}) {
logger.Error(args...)
}
func Errorf(message string, args ...interface{}) {
logger.Errorf(message, args...)
}
func ErrorWithFields(fields Fields, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Error(args...)
}
func ErrorWithFieldsf(fields Fields, message string, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Errorf(message, args...)
}
func NewError(args ...interface{}) error {
Error(args...)
return errors.New(fmt.Sprint(args...))
}
func NewErrorf(message string, args ...interface{}) error {
Errorf(message, args...)
return fmt.Errorf(message, args...)
}
func NewErrorWithFields(fields Fields, args ...interface{}) error {
ErrorWithFields(fields, args...)
return errors.New(fmt.Sprint(args...))
}
func NewErrorWithFieldsf(fields Fields, message string, args ...interface{}) error {
ErrorWithFieldsf(fields, message, args...)
return fmt.Errorf(message, args...)
}
func Warn(args ...interface{}) {
logger.Warn(args...)
}
func Warnf(message string, args ...interface{}) {
logger.Warnf(message, args...)
}
func WarnWithFields(fields Fields, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Warn(args...)
}
func WarnWithFieldsf(fields Fields, message string, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Warnf(message, args...)
}
func Fatal(args ...interface{}) {
logger.Fatal(args...)
}
func Fatalf(message string, args ...interface{}) {
logger.Fatalf(message, args...)
}
func FatalWithFields(fields Fields, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Fatal(args...)
}
func FatalWithFieldsf(fields Fields, message string, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Fatalf(message, args...)
}
func Panic(args ...interface{}) {
logger.Panic(args...)
}
func Panicf(message string, args ...interface{}) {
logger.Panicf(message, args...)
}
func PanicWithFields(fields Fields, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Panic(args...)
}
func PanicWithFieldsf(fields Fields, message string, args ...interface{}) {
logger.WithFields(logrus.Fields(fields)).Panicf(message, args...)
}
// ServerLogger is a middleware that logs the start and end of each request, along
// with some useful data about what was requested, what the response status was,
// and how long it took to return.
func ServerLogger() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
t1 := time.Now()
defer func() {
logger.WithFields(logrus.Fields{
"proto": r.Proto,
"path": r.URL.Path,
"duration": time.Since(t1),
"status": ww.Status(),
"size": ww.BytesWritten(),
"ip": r.RemoteAddr,
"requestID": middleware.GetReqID(r.Context()),
}).Info("Request Served")
}()
next.ServeHTTP(ww, r)
}
return http.HandlerFunc(fn)
}
}