mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
Context based logging for rbd
Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
committed by
mergify[bot]
parent
81c28d6cb0
commit
38ca08bf65
128
pkg/util/log.go
128
pkg/util/log.go
@ -19,133 +19,15 @@ package util
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
type Verbosity struct {
|
||||
level int
|
||||
}
|
||||
|
||||
func V(i int) *Verbosity {
|
||||
return &Verbosity{level: i}
|
||||
}
|
||||
|
||||
type contextKey string
|
||||
|
||||
// Key for context based logging
|
||||
var Key = contextKey("ID")
|
||||
|
||||
var id = "ID: %d "
|
||||
|
||||
// INFO
|
||||
func (v *Verbosity) Infof(ctx context.Context, format string, args ...interface{}) {
|
||||
format = id + format
|
||||
args = append([]interface{}{ctx.Value(Key)}, args...)
|
||||
klog.V(klog.Level(v.level)).Infof(format, args...)
|
||||
}
|
||||
|
||||
func Infof(ctx context.Context, format string, args ...interface{}) {
|
||||
format = id + format
|
||||
args = append([]interface{}{ctx.Value(Key)}, args...)
|
||||
klog.Infof(format, args...)
|
||||
}
|
||||
|
||||
func (v *Verbosity) Info(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf(id, ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.V(klog.Level(v.level)).Info(args...)
|
||||
}
|
||||
|
||||
func Info(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf(id, ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Info(args...)
|
||||
}
|
||||
|
||||
func (v *Verbosity) Infoln(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf("ID: %d", ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.V(klog.Level(v.level)).Infoln(args...)
|
||||
}
|
||||
|
||||
func Infoln(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf("ID: %d", ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Infoln(args...)
|
||||
}
|
||||
|
||||
// WARNING
|
||||
func Warningf(ctx context.Context, format string, args ...interface{}) {
|
||||
format = id + format
|
||||
args = append([]interface{}{ctx.Value(Key)}, args...)
|
||||
klog.Warningf(format, args...)
|
||||
}
|
||||
|
||||
func Warning(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf(id, ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Warning(args...)
|
||||
}
|
||||
|
||||
func Warningln(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf("ID: %d", ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Warningln(args...)
|
||||
}
|
||||
|
||||
// ERROR
|
||||
func Errorf(ctx context.Context, format string, args ...interface{}) {
|
||||
format = id + format
|
||||
args = append([]interface{}{ctx.Value(Key)}, args...)
|
||||
klog.Errorf(format, args...)
|
||||
}
|
||||
|
||||
func Error(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf(id, ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Error(args...)
|
||||
}
|
||||
|
||||
func Errorln(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf("ID: %d", ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Errorln(args...)
|
||||
}
|
||||
|
||||
// FATAL
|
||||
func Fatalf(ctx context.Context, format string, args ...interface{}) {
|
||||
format = id + format
|
||||
args = append([]interface{}{ctx.Value(Key)}, args...)
|
||||
klog.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
func Fatal(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf(id, ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Fatal(args...)
|
||||
}
|
||||
|
||||
func Fatalln(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf("ID: %d", ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Fatalln(args...)
|
||||
}
|
||||
|
||||
// EXIT
|
||||
func Exitf(ctx context.Context, format string, args ...interface{}) {
|
||||
format = id + format
|
||||
args = append([]interface{}{ctx.Value(Key)}, args...)
|
||||
klog.Exitf(format, args...)
|
||||
}
|
||||
|
||||
func Exit(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf(id, ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Exit(args...)
|
||||
}
|
||||
|
||||
func Exitln(ctx context.Context, args ...interface{}) {
|
||||
idString := fmt.Sprintf("ID: %d", ctx.Value(Key))
|
||||
args = append([]interface{}{idString}, args...)
|
||||
klog.Exitln(args...)
|
||||
// Log helps in context based logging
|
||||
func Log(ctx context.Context, format string) string {
|
||||
a := fmt.Sprintf("ID: %v ", ctx.Value(Key))
|
||||
return a + format
|
||||
}
|
||||
|
Reference in New Issue
Block a user