-
Notifications
You must be signed in to change notification settings - Fork 583
/
log.go
43 lines (33 loc) · 865 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package log
import (
"context"
"github.com/sirupsen/logrus"
)
type logKey string
const (
key logKey = "rke-logger"
)
type logger interface {
Debugf(msg string, args ...interface{})
Infof(msg string, args ...interface{})
Warnf(msg string, args ...interface{})
}
func SetLogger(ctx context.Context, logger logger) context.Context {
return context.WithValue(ctx, key, logger)
}
func getLogger(ctx context.Context) logger {
logger, _ := ctx.Value(key).(logger)
if logger == nil {
return logrus.StandardLogger()
}
return logger
}
func Infof(ctx context.Context, msg string, args ...interface{}) {
getLogger(ctx).Infof(msg, args...)
}
func Warnf(ctx context.Context, msg string, args ...interface{}) {
getLogger(ctx).Warnf(msg, args...)
}
func Debugf(ctx context.Context, msg string, args ...interface{}) {
getLogger(ctx).Debugf(msg, args...)
}