|
| 1 | +package ginoauth2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/golang/glog" |
| 9 | +) |
| 10 | + |
| 11 | +// Logger is the interface used by GinOAuth2 to log messages. |
| 12 | +type Logger interface { |
| 13 | + Errorf(format string, args ...interface{}) |
| 14 | + Infof(format string, args ...interface{}) |
| 15 | + Debugf(format string, args ...interface{}) |
| 16 | +} |
| 17 | + |
| 18 | +type glogLogger struct { |
| 19 | + output io.Writer |
| 20 | +} |
| 21 | + |
| 22 | +// DefaultLogger is the default logger used by GinOAuth2 if no other logger is provided. |
| 23 | +// To use a different logger, set the DefaultLogger variable to a logger of your choice. |
| 24 | +// Replacement loggers must implement the Logger interface. |
| 25 | +// |
| 26 | +// Example: |
| 27 | +// |
| 28 | +// import "github.com/zalando/gin-oauth2" |
| 29 | +// |
| 30 | +// ginoauth2.DefaultLogger = &logrusLogger{} // use logrus |
| 31 | +var DefaultLogger Logger = &glogLogger{output: os.Stderr} |
| 32 | + |
| 33 | +func maskLogArgs(args ...interface{}) []interface{} { |
| 34 | + for i := range args { |
| 35 | + args[i] = maskAccessToken(args[i]) |
| 36 | + } |
| 37 | + |
| 38 | + return args |
| 39 | +} |
| 40 | + |
| 41 | +// SetOutput sets the output destination for the logger |
| 42 | +func (gl *glogLogger) setOutput(w io.Writer) { |
| 43 | + gl.output = w |
| 44 | +} |
| 45 | + |
| 46 | +// Errorf is a logging function using glog.Errorf |
| 47 | +func (gl *glogLogger) Errorf(f string, args ...interface{}) { |
| 48 | + glog.ErrorDepth(1, fmt.Sprintf(f, args...)) |
| 49 | + if gl.output != nil { |
| 50 | + fmt.Fprintf(gl.output, f+"\n", args...) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Infof is a logging function using glog.Infof |
| 55 | +func (gl *glogLogger) Infof(f string, args ...interface{}) { |
| 56 | + glog.InfoDepth(1, fmt.Sprintf(f, args...)) |
| 57 | + if gl.output != nil { |
| 58 | + fmt.Fprintf(gl.output, f+"\n", args...) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Debugf is a verbose logging function using glog.V(2) |
| 63 | +func (gl *glogLogger) Debugf(f string, args ...interface{}) { |
| 64 | + if glog.V(2) { |
| 65 | + glog.InfoDepth(1, fmt.Sprintf(f, args...)) |
| 66 | + } |
| 67 | + if gl.output != nil { |
| 68 | + fmt.Fprintf(gl.output, f+"\n", args...) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func errorf(f string, args ...interface{}) { |
| 73 | + DefaultLogger.Errorf(f, maskLogArgs(args...)...) |
| 74 | +} |
| 75 | + |
| 76 | +func infof(f string, args ...interface{}) { |
| 77 | + DefaultLogger.Infof(f, maskLogArgs(args...)...) |
| 78 | +} |
| 79 | + |
| 80 | +func infofv2(f string, args ...interface{}) { |
| 81 | + DefaultLogger.Debugf(f, maskLogArgs(args...)...) |
| 82 | +} |
0 commit comments