Skip to content

Commit

Permalink
switch to full path of general logging (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynepeking348 committed Jul 5, 2023
1 parent 3e01845 commit 98962c5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
38 changes: 36 additions & 2 deletions pkg/util/general/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
LoggingPKGFull
)

const skippedPackagePrefix = "github.com/kubewharf/"

// loggingWithDepth returns the logging-prefix for caller.
// it will help to avoid hardcode function names in logging
// message especially for cases that function names are changed.
Expand All @@ -59,43 +61,75 @@ func loggingWithDepth(pkg LoggingPKG) string {
case LoggingPKGShort:
return callerNames[len(callerNames)-1]
case LoggingPKGFull:
return callPath
return strings.TrimPrefix(callPath, skippedPackagePrefix)
}
return ""
}

func logging(message string, params ...interface{}) string {
return "[" + loggingWithDepth(LoggingPKGShort) + "] " + fmt.Sprintf(message, params...)
return "[" + loggingWithDepth(LoggingPKGFull) + "] " + fmt.Sprintf(message, params...)
}

func loggingPath(pkg LoggingPKG, message string, params ...interface{}) string {
return "[" + loggingWithDepth(pkg) + "] " + fmt.Sprintf(message, params...)
}

func InfoS(message string, params ...interface{}) {
klog.InfoSDepth(1, logging(message), params...)
}

func InfoSPath(pkg LoggingPKG, message string, params ...interface{}) {
klog.InfoSDepth(1, loggingPath(pkg, message), params...)
}

func Infof(message string, params ...interface{}) {
klog.InfofDepth(1, logging(message, params...))
}

func InfofPath(pkg LoggingPKG, message string, params ...interface{}) {
klog.InfofDepth(1, loggingPath(pkg, message, params...))
}

func InfofV(level int, message string, params ...interface{}) {
klog.V(klog.Level(level)).InfofDepth(1, logging(message, params...))
}

func InfofVPath(pkg LoggingPKG, level int, message string, params ...interface{}) {
klog.V(klog.Level(level)).InfofDepth(1, loggingPath(pkg, message, params...))
}

func Warningf(message string, params ...interface{}) {
klog.WarningfDepth(1, logging(message, params...))
}

func WarningfPath(pkg LoggingPKG, message string, params ...interface{}) {
klog.WarningfDepth(1, loggingPath(pkg, message, params...))
}

func Errorf(message string, params ...interface{}) {
klog.ErrorfDepth(1, logging(message, params...))
}

func ErrorfPath(pkg LoggingPKG, message string, params ...interface{}) {
klog.ErrorfDepth(1, loggingPath(pkg, message, params...))
}

func ErrorS(err error, message string, params ...interface{}) {
klog.ErrorSDepth(1, err, logging(message), params...)
}

func ErrorSPath(pkg LoggingPKG, err error, message string, params ...interface{}) {
klog.ErrorSDepth(1, err, loggingPath(pkg, message), params...)
}

func Fatalf(message string, params ...interface{}) {
klog.FatalfDepth(1, logging(message, params...))
}

func FatalfPath(pkg LoggingPKG, message string, params ...interface{}) {
klog.FatalfDepth(1, loggingPath(pkg, message, params...))
}

type Logger struct {
pkg LoggingPKG
prefix string
Expand Down
18 changes: 16 additions & 2 deletions pkg/util/general/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (_ testLogging) log(message string, params ...interface{}) string {
return logging(message, params...)
}

func (_ testLogging) logPath(pkg LoggingPKG, message string, params ...interface{}) string {
return loggingPath(pkg, message, params...)
}

type testLogger struct {
klog Logger // klog for katalyst-log
}
Expand All @@ -43,7 +47,10 @@ func TestLogging(t *testing.T) {
require.Equal(t, "[testing.tRunner] extra 1 test", loggingWithoutStruct)

loggingWithStruct := testLogging{}.log("extra %v %v", 1, "test")
require.Equal(t, "[general.TestLogging] extra 1 test", loggingWithStruct)
require.Equal(t, "[katalyst-core/pkg/util/general.TestLogging] extra 1 test", loggingWithStruct)

loggingPathWithStruct := testLogging{}.logPath(LoggingPKGShort, "extra %v %v", 1, "test")
require.Equal(t, "[general.TestLogging] extra 1 test", loggingPathWithStruct)

loggerWithoutStruct := LoggerWithPrefix("p-test", LoggingPKGNone).logging("extra %v %v", 1, "test")
require.Equal(t, "[p-test: tRunner] extra 1 test", loggerWithoutStruct)
Expand All @@ -61,7 +68,7 @@ func TestLogging(t *testing.T) {
require.Equal(t, "[p-test: general.TestLogging] extra 1 test", loggerWithStruct)

loggerWithStruct = testLogger{klog: LoggerWithPrefix("p-test", LoggingPKGFull)}.log("extra %v %v", 1, "test")
require.Equal(t, "[p-test: github.com/kubewharf/katalyst-core/pkg/util/general.TestLogging] extra 1 test", loggerWithStruct)
require.Equal(t, "[p-test: katalyst-core/pkg/util/general.TestLogging] extra 1 test", loggerWithStruct)

InfoS("test-InfoS", "param-key", "param-InfoS")
Infof("test-Infof %v", "extra-Infof")
Expand All @@ -70,6 +77,13 @@ func TestLogging(t *testing.T) {
Errorf("test-Errorf %v", "extra-Errorf")
ErrorS(fmt.Errorf("err"), "test-ErrorS", "param-key", "param-ErrorS")

InfoSPath(LoggingPKGShort, "test-InfoS", "param-key", "param-InfoS")
InfofPath(LoggingPKGShort, "test-Infof %v", "extra-Infof")
InfofVPath(LoggingPKGShort, 1, "test-InfofV %v", "extra-InfofV")
WarningfPath(LoggingPKGShort, "test-Warningf %v", "extra-Warningf")
ErrorfPath(LoggingPKGShort, "test-Errorf %v", "extra-Errorf")
ErrorSPath(LoggingPKGShort, fmt.Errorf("err"), "test-ErrorS", "param-key", "param-ErrorS")

go func() {
goStr := logging("extra %v %v", 1, "test")
require.Equal(t, "[runtime.goexit] extra 1 test", goStr)
Expand Down

0 comments on commit 98962c5

Please sign in to comment.