-
Notifications
You must be signed in to change notification settings - Fork 32
/
log.go
333 lines (286 loc) · 9.53 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package log
import (
"bytes"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/codeready-toolchain/registration-service/pkg/configuration"
"github.com/codeready-toolchain/registration-service/pkg/context"
"github.com/labstack/echo/v4"
"github.com/gin-gonic/gin"
"github.com/go-logr/logr"
sync "github.com/matryer/resync"
"github.com/spf13/pflag"
klogv1 "k8s.io/klog"
klogv2 "k8s.io/klog/v2"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var (
logger *Logger
once sync.Once
)
// Logger implements log.Logger
type Logger struct {
logr logr.Logger
name string
}
// Init initializes the logger.
func Init(withName string, opts ...zap.Opts) {
once.Do(func() {
zapFlagSet := pflag.NewFlagSet("zap", pflag.ExitOnError)
// Add the zap logger flag set to the CLI. The flag set must
// be added before calling pflag.Parse().
pflag.CommandLine.AddFlagSet(zapFlagSet)
// Add flags registered by imported packages (e.g. glog and
// controller-runtime)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// Use a zap logr.Logger implementation. If none of the zap
// flags are configured (or if the zap flag set is not being
// used), this defaults to a production zap logger.
//
// The logger instantiated here can be changed to any logger
// implementing the logr.Logger interface. This logger will
// be propagated through the whole operator, generating
// uniform and structured logs.
logf.SetLogger(zap.New(opts...))
logger = newLogger(withName)
// also set the client-go logger so we get the same JSON output
klogv2.SetLogger(zap.New(opts...))
// see https://github.com/kubernetes/klog#coexisting-with-klogv2
// BEGIN : hack to redirect klogv1 calls to klog v2
// Tell klog NOT to log into STDERR. Otherwise, we risk
// certain kinds of API errors getting logged into a directory not
// available in a `FROM scratch` Docker container, causing us to abort
var klogv1Flags flag.FlagSet
klogv1.InitFlags(&klogv1Flags)
setupLog := logf.Log.WithName("setup")
if err := klogv1Flags.Set("logtostderr", "false"); err != nil { // By default klog v1 logs to stderr, switch that off
setupLog.Error(err, "")
os.Exit(1)
}
if err := klogv1Flags.Set("stderrthreshold", "FATAL"); err != nil { // stderrthreshold defaults to ERROR, so we don't get anything in stderr
setupLog.Error(err, "")
os.Exit(1)
}
klogv1.SetOutputBySeverity("INFO", klogWriter{}) // tell klog v1 to use the custom writer
// END : hack to redirect klogv1 calls to klog v2
})
}
func newLogger(withName string) *Logger {
return &Logger{
logr: logf.Log.WithName(withName),
name: withName,
}
}
// Info logs a non-error message.
func Info(ctx *gin.Context, msg string) {
logger.Info(ctx, msg)
}
// Infof logs a non-error formatted message.
func Infof(ctx *gin.Context, msg string, args ...string) {
logger.Infof(ctx, msg, args...)
}
// InfoEchof logs a non-error formatted message for echo events.
func InfoEchof(ctx echo.Context, msg string, args ...string) {
logger.InfoEchof(ctx, msg, args...)
}
// Error logs the error with the given message.
func Error(ctx *gin.Context, err error, msg string) {
logger.Error(ctx, err, msg)
}
// Errorf logs the error with the given formatted message.
func Errorf(ctx *gin.Context, err error, msg string, args ...string) {
logger.Errorf(ctx, err, msg, args...)
}
// WithValues creates a new logger with additional key-value pairs in the context
func WithValues(keysAndValues map[string]interface{}) *Logger {
return logger.WithValues(keysAndValues)
}
// Info logs a non-error message.
func (l *Logger) Info(ctx *gin.Context, msg string) {
ctxInfo := addContextInfo(ctx)
l.logr.Info(msg, ctxInfo...)
}
// Infof logs a non-error formatted message.
func (l *Logger) Infof(ctx *gin.Context, msg string, args ...string) {
ctxInfo := addContextInfo(ctx)
l.infof(ctxInfo, msg, args...)
}
// InfoEchof logs a non-error formatted message for echo events.
func (l *Logger) InfoEchof(ctx echo.Context, msg string, args ...string) {
userID, _ := ctx.Get(context.SubKey).(string)
username, _ := ctx.Get(context.UsernameKey).(string)
ctxFields := genericContext(userID, username)
workspace, _ := ctx.Get(context.WorkspaceKey).(string)
ctxFields = append(ctxFields, "workspace")
ctxFields = append(ctxFields, workspace)
ctxFields = append(ctxFields, "method")
ctxFields = append(ctxFields, ctx.Request().Method)
ctxFields = append(ctxFields, "url")
ctxFields = append(ctxFields, ctx.Request().URL)
l.infof(ctxFields, msg, args...)
}
func (l *Logger) infof(ctx []interface{}, msg string, args ...string) {
arguments := make([]interface{}, len(args))
for i, arg := range args {
arguments[i] = arg
}
if len(arguments) > 0 {
l.logr.Info(fmt.Sprintf(msg, arguments...), ctx...)
} else {
l.logr.Info(msg, ctx...)
}
}
// Error logs the error with the given message.
func (l *Logger) Error(ctx *gin.Context, err error, msg string) {
l.Errorf(ctx, err, msg)
}
// Errorf logs the error with the given formatted message.
func (l *Logger) Errorf(ctx *gin.Context, err error, msg string, args ...string) {
ctxInfo := addContextInfo(ctx)
arguments := make([]interface{}, len(args))
for i, arg := range args {
arguments[i] = arg
}
if len(arguments) > 0 {
l.logr.Error(err, fmt.Sprintf(msg, arguments...), ctxInfo...)
} else {
l.logr.Error(err, msg, ctxInfo...)
}
}
// WithValues creates a new logger with additional key-value pairs in the context
func (l *Logger) WithValues(keysAndValues map[string]interface{}) *Logger {
if len(keysAndValues) > 0 {
nl := newLogger(logger.name)
nl.logr = nl.logr.WithValues(slice(keysAndValues)...)
return nl
}
return l
}
func slice(keysAndValues map[string]interface{}) []interface{} {
tags := make([]interface{}, 0, len(keysAndValues)*2)
for k, v := range keysAndValues {
tags = append(tags, k)
tags = append(tags, v)
}
return tags
}
// addContextInfo adds fields extracted from the context to the info/error
// log messages.
func addContextInfo(ctx *gin.Context) []interface{} {
if ctx != nil {
subject := ctx.GetString(context.SubKey)
username := ctx.GetString(context.UsernameKey)
fields := genericContext(subject, username)
if ctx.Request != nil {
fields = append(fields, addRequestInfo(ctx.Request)...)
}
return fields
}
return genericContext("", "")
}
func genericContext(subject, username string) []interface{} {
var fields []interface{}
// TODO: we probably don't need the timestamp as it is automatically added to the log by the logger
currentTime := time.Now()
fields = append(fields, "timestamp")
fields = append(fields, currentTime.Format(time.RFC1123Z))
// TODO: we can drop the commit as well - printing out the commit for every single line is a kind of overkill
fields = append(fields, "commit")
if len(configuration.Commit) > 7 {
fields = append(fields, configuration.Commit[0:7])
} else {
fields = append(fields, configuration.Commit)
}
if subject != "" {
fields = append(fields, "user_id")
fields = append(fields, subject)
}
if username != "" {
fields = append(fields, context.UsernameKey)
fields = append(fields, username)
}
return fields
}
// addRequestInfo adds fields extracted from context.Request.
func addRequestInfo(req *http.Request) []interface{} {
var fields []interface{}
url := req.URL
if url != nil {
fields = append(fields, "req_url")
fields = append(fields, fmt.Sprintf("%s://%s%s", url.Scheme, url.Host, url.Path))
reqParams := req.URL.Query()
if len(reqParams) > 0 {
params := make(map[string][]string)
for name, values := range reqParams {
if strings.ToLower(name) != "token" {
params[name] = values
} else {
// Hide sensitive information
params[name] = []string{"*****"}
}
}
fields = append(fields, "req_params")
fields = append(fields, params)
}
}
if len(req.Header) > 0 {
headers := make(map[string]interface{}, len(req.Header))
for k, v := range req.Header {
// Hide sensitive information
if k == "Authorization" || k == "Cookie" {
headers[k] = "*****"
} else {
headers[k] = v
}
}
fields = append(fields, "req_headers")
fields = append(fields, headers)
}
if req.ContentLength > 0 {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(req.Body)
var newStr string
if err != nil {
newStr = "<invalid data>"
} else {
newStr = buf.String()
}
fields = append(fields, "req_payload")
fields = append(fields, newStr)
// Once req.Body is read, it is empty. Restore its contents by
// assigning a new reader with the same contents.
req.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
}
return fields
}
// OutputCallDepth is the stack depth where we can find the origin of this call
const OutputCallDepth = 6
// DefaultPrefixLength is the length of the log prefix that we have to strip out
const DefaultPrefixLength = 53
// klogWriter is used in SetOutputBySeverity call below to redirect
// any calls to klogv1 to end up in klogv2
type klogWriter struct{}
func (kw klogWriter) Write(p []byte) (n int, err error) {
if len(p) < DefaultPrefixLength {
klogv2.InfoDepth(OutputCallDepth, string(p))
return len(p), nil
}
if p[0] == 'I' {
klogv2.InfoDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else if p[0] == 'W' {
klogv2.WarningDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else if p[0] == 'E' {
klogv2.ErrorDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else if p[0] == 'F' {
klogv2.FatalDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else {
klogv2.InfoDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
}
return len(p), nil
}