forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
71 lines (59 loc) · 1.8 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
package authboss
import (
"context"
"fmt"
"net/http"
)
// Logger is the basic logging structure that's required
type Logger interface {
Info(string)
Error(string)
}
// ContextLogger creates a logger from a request context
type ContextLogger interface {
FromContext(context.Context) Logger
}
// RequestLogger creates a logger from a request
type RequestLogger interface {
FromRequest(*http.Request) Logger
}
// RequestLogger returns a request logger if possible, if not
// it calls Logger which tries to do a ContextLogger, and if
// that fails it will finally get a normal logger.
func (a *Authboss) RequestLogger(r *http.Request) FmtLogger {
logger := a.Config.Core.Logger
if reqLogger, ok := logger.(RequestLogger); ok {
return FmtLogger{reqLogger.FromRequest(r)}
}
return FmtLogger{a.Logger(r.Context())}
}
// Logger returns an appopriate logger for the context:
// If context is nil, then it simply returns the configured
// logger.
// If context is not nil, then it will attempt to upgrade
// the configured logger to a ContextLogger, and create
// a context-specific logger for use.
func (a *Authboss) Logger(ctx context.Context) FmtLogger {
logger := a.Config.Core.Logger
if ctx == nil {
return FmtLogger{logger}
}
ctxLogger, ok := logger.(ContextLogger)
if !ok {
return FmtLogger{logger}
}
return FmtLogger{ctxLogger.FromContext(ctx)}
}
// FmtLogger adds convenience functions on top of the logging
// methods for formatting.
type FmtLogger struct {
Logger
}
// Errorf prints to Error() with fmt.Printf semantics
func (f FmtLogger) Errorf(format string, values ...interface{}) {
f.Logger.Error(fmt.Sprintf(format, values...))
}
// Infof prints to Info() with fmt.Printf semantics
func (f FmtLogger) Infof(format string, values ...interface{}) {
f.Logger.Info(fmt.Sprintf(format, values...))
}