From 7721e540e2c016c95611341ec97ae04a84c78587 Mon Sep 17 00:00:00 2001 From: Changxin Miao Date: Tue, 26 Nov 2024 21:33:00 +0800 Subject: [PATCH] Output method name in log (#5322) Signed-off-by: Changxin Miao --- pkg/utils/logger.go | 31 ++++++++++++++++++++++++++++++- pkg/utils/logger_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/pkg/utils/logger.go b/pkg/utils/logger.go index 5d6a5cd50110..2a686c4f7142 100644 --- a/pkg/utils/logger.go +++ b/pkg/utils/logger.go @@ -61,13 +61,14 @@ func (l *logHandle) Format(e *logrus.Entry) ([]byte, error) { } const timeFormat = "2006/01/02 15:04:05.000000" timestamp := e.Time.Format(timeFormat) - str := fmt.Sprintf("%s%v %s[%d] <%v>: %v [%s:%d]", + str := fmt.Sprintf("%s%v %s[%d] <%v>: %v [%s@%s:%d]", l.logid, timestamp, l.name, os.Getpid(), lvlStr, strings.TrimRight(e.Message, "\n"), + methodName(e.Caller.Function), path.Base(e.Caller.File), e.Caller.Line) @@ -80,6 +81,34 @@ func (l *logHandle) Format(e *logrus.Entry) ([]byte, error) { return []byte(str), nil } +// Returns a human-readable method name, removing internal markers added by Go +func methodName(fullFuncName string) string { + firstSlash := strings.Index(fullFuncName, "/") + if firstSlash != -1 && firstSlash < len(fullFuncName)-1 { + fullFuncName = fullFuncName[firstSlash+1:] + } + lastDot := strings.LastIndex(fullFuncName, ".") + if lastDot == -1 || lastDot == len(fullFuncName)-1 { + return fullFuncName + } + method := fullFuncName[lastDot+1:] + // avoid func1 + if strings.HasPrefix(method, "func") && method[4] >= '0' && method[4] <= '9' { + candidate := methodName(fullFuncName[:lastDot]) + if candidate != "" { + method = candidate + } + } + // aoid init.3 + if len(method) == 1 && method[0] >= '0' && method[0] <= '9' { + candidate := methodName(fullFuncName[:lastDot]) + if candidate != "" { + method = candidate + } + } + return method +} + // for aws.Logger func (l *logHandle) Log(args ...interface{}) { l.Debugln(args...) diff --git a/pkg/utils/logger_test.go b/pkg/utils/logger_test.go index 87439a6a2a42..4d4e1d025809 100644 --- a/pkg/utils/logger_test.go +++ b/pkg/utils/logger_test.go @@ -58,3 +58,39 @@ func TestLogger(t *testing.T) { t.Fatalf("logid \"testid\" should be logged: %s", s) } } + +func TestMethodName(t *testing.T) { + type args struct { + fullFuncName string + } + tests := []struct { + name string + args args + want string + }{{ + name: "main", + args: args{ + fullFuncName: "cmd.Main", + }, + want: "Main", + }, { + name: "nested method", + args: args{ + fullFuncName: "github.com/juicedata/juicefs/cmd.watchdog.func1", + }, + want: "watchdog", + }, { + name: "multiple inits", + args: args{ + fullFuncName: "github.com/juicedata/juicefs/pkg/utils.init.3.func1", + }, + want: "init", + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := methodName(tt.args.fullFuncName); got != tt.want { + t.Errorf("methodName() = %v, want %v", got, tt.want) + } + }) + } +}