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:
39
log/log.go
Normal file
39
log/log.go
Normal 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...)
|
||||
}
|
Reference in New Issue
Block a user