-
Notifications
You must be signed in to change notification settings - Fork 14
/
logger.go
61 lines (49 loc) · 1.6 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
package jwtauth
import (
"context"
"log"
)
var pkgLogger Logger = defaultLogger{}
// SetLogger allows users of this library to set a logger to record non-critical errors.
//
// eg. Failure to pre-fill the JWKS cache
//
// If SetLogger is not called, logging will be sent to the default go log.
func SetLogger(l Logger) {
pkgLogger = l
}
// XXX_GetLogger exposes the jwtauth package logger for use in auth middleware generated by
// protoc-gen-go-jwtauth.
//
// DO NOT use this to get the logger yourself.
func XXX_GetLogger() Logger { //nolint
return pkgLogger
}
// SetLogFuncs sets the log functions to be used by the jwtauth logger.
func SetLogFuncs(debug func(ctx context.Context, args ...interface{}), debugf func(ctx context.Context, format string, args ...interface{})) {
pkgLogger = &FuncLogger{
DebugFunc: debug,
DebugfFunc: debugf,
}
}
type Logger interface {
Debug(ctx context.Context, args ...interface{})
Debugf(ctx context.Context, format string, args ...interface{})
}
type defaultLogger struct{}
func (defaultLogger) Debug(ctx context.Context, args ...interface{}) {
log.Println(args...)
}
func (defaultLogger) Debugf(ctx context.Context, format string, args ...interface{}) {
log.Printf(format, args...)
}
type FuncLogger struct {
DebugFunc func(ctx context.Context, args ...interface{})
DebugfFunc func(ctx context.Context, format string, args ...interface{})
}
func (f *FuncLogger) Debug(ctx context.Context, args ...interface{}) {
f.DebugFunc(ctx, args...)
}
func (f *FuncLogger) Debugf(ctx context.Context, format string, args ...interface{}) {
f.DebugfFunc(ctx, format, args...)
}