Skip to content

Commit

Permalink
change log format to export the entire pkg name (kubewharf#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynepeking348 authored and luomingmeng committed Oct 11, 2024
1 parent 9a1dc54 commit 3cd02f0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
9 changes: 3 additions & 6 deletions pkg/util/general/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

const callDepth = 3
const pkgPrefix = "github.com/kubewharf/"

// loggingWithDepth returns the logging-prefix for caller.
// it will help to avoid hardcode function names in logging
Expand All @@ -35,12 +36,8 @@ func loggingWithDepth() string {
return ""
}

funcPaths := strings.Split(runtime.FuncForPC(pc).Name(), "/")
if len(funcPaths) == 0 {
return ""
}

funcNames := strings.Split(funcPaths[len(funcPaths)-1], ".")
funcPaths := strings.TrimPrefix(runtime.FuncForPC(pc).Name(), pkgPrefix)
funcNames := strings.Split(funcPaths, ".")
switch len(funcNames) {
case 0, 1:
case 2:
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/general/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLoggingPrefix(t *testing.T) {
require.Equal(t, "[testing/tRunner] extra 1 test", withoutStruct)

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

InfoS("test-InfoS", "param-key", "param-InfoS")
Infof("test-Infof %v", "extra-Infof")
Expand Down

0 comments on commit 3cd02f0

Please sign in to comment.