From 81c28d6cb0a6d64c4c84c90724275192eb64028f Mon Sep 17 00:00:00 2001 From: Daniel-Pivonka Date: Wed, 14 Aug 2019 14:45:30 -0400 Subject: [PATCH] implement klog wrapper Signed-off-by: Daniel-Pivonka --- pkg/csi-common/utils.go | 13 ++-- pkg/rbd/nodeserver.go | 10 +-- pkg/util/log.go | 151 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 pkg/util/log.go diff --git a/pkg/csi-common/utils.go b/pkg/csi-common/utils.go index 78b1d1735..0e1193219 100644 --- a/pkg/csi-common/utils.go +++ b/pkg/csi-common/utils.go @@ -22,6 +22,7 @@ import ( "strings" "sync/atomic" + "github.com/ceph/ceph-csi/pkg/util" "github.com/container-storage-interface/spec/lib/go/csi" "github.com/kubernetes-csi/csi-lib-utils/protosanitizer" "golang.org/x/net/context" @@ -109,19 +110,19 @@ func RunControllerandNodePublishServer(endpoint string, d *CSIDriver, cs csi.Con var id uint64 func contextIDInjector(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { - id = atomic.AddUint64(&id, 1) - ctx = context.WithValue(ctx, "ID", id) + atomic.AddUint64(&id, 1) + ctx = context.WithValue(ctx, util.Key, id) return handler(ctx, req) } func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { - klog.V(3).Infof("ID: %d GRPC call: %s", ctx.Value("ID"), info.FullMethod) - klog.V(5).Infof("ID: %d GRPC request: %s", ctx.Value("ID"), protosanitizer.StripSecrets(req)) + util.V(3).Infof(ctx, "GRPC call: %s", info.FullMethod) + util.V(5).Infof(ctx, "GRPC request: %s", protosanitizer.StripSecrets(req)) resp, err := handler(ctx, req) if err != nil { - klog.Errorf("ID: %d GRPC error: %v", ctx.Value("ID"), err) + util.Errorf(ctx, "GRPC error: %v", err) } else { - klog.V(5).Infof("ID: %d GRPC response: %s", ctx.Value("ID"), protosanitizer.StripSecrets(resp)) + util.V(5).Infof(ctx, "GRPC response: %s", protosanitizer.StripSecrets(resp)) } return resp, err } diff --git a/pkg/rbd/nodeserver.go b/pkg/rbd/nodeserver.go index 19cd9e78f..fa8b9c1dc 100644 --- a/pkg/rbd/nodeserver.go +++ b/pkg/rbd/nodeserver.go @@ -258,7 +258,7 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis return nil, err } - klog.Infof("ID: %d rbd: successfully mounted stagingPath %s to targetPath %s", ctx.Value("ID"), stagingPath, targetPath) + util.Infof(ctx, "rbd: successfully mounted stagingPath %s to targetPath %s", stagingPath, targetPath) return &csi.NodePublishVolumeResponse{}, nil } @@ -325,8 +325,8 @@ func (ns *NodeServer) mountVolume(ctx context.Context, stagingPath string, req * mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags() isBlock := req.GetVolumeCapability().GetBlock() != nil targetPath := req.GetTargetPath() - klog.V(4).Infof("ID: %d target %v\nisBlock %v\nfstype %v\nstagingPath %v\nreadonly %v\nmountflags %v\n", - ctx.Value("ID"), targetPath, isBlock, fsType, stagingPath, readOnly, mountFlags) + util.V(4).Infof(ctx, "target %v\nisBlock %v\nfstype %v\nstagingPath %v\nreadonly %v\nmountflags %v\n", + targetPath, isBlock, fsType, stagingPath, readOnly, mountFlags) mountFlags = append(mountFlags, "bind") if readOnly { mountFlags = append(mountFlags, "ro") @@ -347,11 +347,11 @@ func (ns *NodeServer) createTargetMountPath(ctx context.Context, mountPath strin // #nosec pathFile, e := os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750) if e != nil { - klog.V(4).Infof("ID: %d Failed to create mountPath:%s with error: %v", ctx.Value("ID"), mountPath, err) + util.V(4).Infof(ctx, "Failed to create mountPath:%s with error: %v", mountPath, err) return notMnt, status.Error(codes.Internal, e.Error()) } if err = pathFile.Close(); err != nil { - klog.V(4).Infof("ID: %d Failed to close mountPath:%s with error: %v", ctx.Value("ID"), mountPath, err) + util.V(4).Infof(ctx, "Failed to close mountPath:%s with error: %v", mountPath, err) return notMnt, status.Error(codes.Internal, err.Error()) } } else { diff --git a/pkg/util/log.go b/pkg/util/log.go new file mode 100644 index 000000000..514ae6f1c --- /dev/null +++ b/pkg/util/log.go @@ -0,0 +1,151 @@ +/* +Copyright 2019 The Ceph-CSI Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +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 + +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...) +}