1
0
mirror of https://github.com/rancher/rke.git synced 2025-09-08 02:20:13 +00:00

Add context.Context to everything and also make logging pluggable

This commit is contained in:
Darren Shepherd
2018-01-09 15:10:56 -07:00
parent 6dbb6cba1d
commit d8dd29765f
30 changed files with 448 additions and 365 deletions

39
log/log.go Normal file
View File

@@ -0,0 +1,39 @@
package log
import (
"context"
"github.com/sirupsen/logrus"
)
type logKey string
const (
key logKey = "rke-logger"
)
type logger 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...)
}