implement klog wrapper

Signed-off-by: Daniel-Pivonka <dpivonka@redhat.com>
This commit is contained in:
Daniel-Pivonka 2019-08-14 14:45:30 -04:00 committed by mergify[bot]
parent aa74f8c87f
commit 81c28d6cb0
3 changed files with 163 additions and 11 deletions

View File

@ -22,6 +22,7 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"github.com/ceph/ceph-csi/pkg/util"
"github.com/container-storage-interface/spec/lib/go/csi" "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer" "github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -109,19 +110,19 @@ func RunControllerandNodePublishServer(endpoint string, d *CSIDriver, cs csi.Con
var id uint64 var id uint64
func contextIDInjector(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { func contextIDInjector(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
id = atomic.AddUint64(&id, 1) atomic.AddUint64(&id, 1)
ctx = context.WithValue(ctx, "ID", id) ctx = context.WithValue(ctx, util.Key, id)
return handler(ctx, req) return handler(ctx, req)
} }
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { 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) util.V(3).Infof(ctx, "GRPC call: %s", info.FullMethod)
klog.V(5).Infof("ID: %d GRPC request: %s", ctx.Value("ID"), protosanitizer.StripSecrets(req)) util.V(5).Infof(ctx, "GRPC request: %s", protosanitizer.StripSecrets(req))
resp, err := handler(ctx, req) resp, err := handler(ctx, req)
if err != nil { if err != nil {
klog.Errorf("ID: %d GRPC error: %v", ctx.Value("ID"), err) util.Errorf(ctx, "GRPC error: %v", err)
} else { } 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 return resp, err
} }

View File

@ -258,7 +258,7 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
return nil, err 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 return &csi.NodePublishVolumeResponse{}, nil
} }
@ -325,8 +325,8 @@ func (ns *NodeServer) mountVolume(ctx context.Context, stagingPath string, req *
mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags() mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags()
isBlock := req.GetVolumeCapability().GetBlock() != nil isBlock := req.GetVolumeCapability().GetBlock() != nil
targetPath := req.GetTargetPath() targetPath := req.GetTargetPath()
klog.V(4).Infof("ID: %d target %v\nisBlock %v\nfstype %v\nstagingPath %v\nreadonly %v\nmountflags %v\n", util.V(4).Infof(ctx, "target %v\nisBlock %v\nfstype %v\nstagingPath %v\nreadonly %v\nmountflags %v\n",
ctx.Value("ID"), targetPath, isBlock, fsType, stagingPath, readOnly, mountFlags) targetPath, isBlock, fsType, stagingPath, readOnly, mountFlags)
mountFlags = append(mountFlags, "bind") mountFlags = append(mountFlags, "bind")
if readOnly { if readOnly {
mountFlags = append(mountFlags, "ro") mountFlags = append(mountFlags, "ro")
@ -347,11 +347,11 @@ func (ns *NodeServer) createTargetMountPath(ctx context.Context, mountPath strin
// #nosec // #nosec
pathFile, e := os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750) pathFile, e := os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750)
if e != nil { 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()) return notMnt, status.Error(codes.Internal, e.Error())
} }
if err = pathFile.Close(); err != nil { 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()) return notMnt, status.Error(codes.Internal, err.Error())
} }
} else { } else {

151
pkg/util/log.go Normal file
View File

@ -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...)
}