-
Notifications
You must be signed in to change notification settings - Fork 5
/
log.go
28 lines (22 loc) · 633 Bytes
/
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
package lastpass
import "context"
type key int
const (
loggerKey key = iota
)
// Logger is the interface which wraps the Printf method.
type Logger interface {
Printf(format string, v ...interface{})
}
// NewContextWithLogger returns a new context with logging enabled.
func NewContextWithLogger(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, loggerKey, logger)
}
func (c *Client) log(ctx context.Context, format string, v ...interface{}) {
if logger, ok := ctx.Value(loggerKey).(Logger); ok {
logger.Printf(format, v...)
}
if c.logger != nil {
c.logger.Printf(format, v...)
}
}