-
Notifications
You must be signed in to change notification settings - Fork 408
Fix logCallerTrimmer when repo name is not just lakefs #9609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "os" | ||
| "reflect" | ||
| "regexp" | ||
| "runtime" | ||
| "slices" | ||
| "strings" | ||
|
|
@@ -76,9 +76,10 @@ type Fields map[string]interface{} | |
|
|
||
| // logCallerTrimmer is used to trim the caller paths to be relative to the project root | ||
| func logCallerTrimmer(frame *runtime.Frame) (function string, file string) { | ||
| indexOfModule := strings.Index(strings.ToLower(frame.File), ProjectDirectoryName) | ||
| if indexOfModule != -1 { | ||
| file = frame.File[indexOfModule+len(ProjectDirectoryName):] | ||
| r := regexp.MustCompile(fmt.Sprintf(`.*/([^/]*%s[^/]*)/.*`, ProjectDirectoryName)) | ||
| matches := r.FindStringSubmatch(strings.ToLower(frame.File)) | ||
| if len(matches) > 1 { | ||
| file = matches[1] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, this solution doesn’t achieve the desired result. From the Go docs: FindStringSubmatch returns a slice of strings containing the text of the leftmost match of the regular expression in s and the matches, if any, of its subexpressions, as described in the “Submatch” section of the package comment. (See an example in the docs) Applying this to our case: For the path: So the resulting file value would be: When the expected result should be: Please make sure your solution also correctly handles paths like: "/Users/annaseliverstov/work/lakefs-anna/cmd/lakefs/cmd/run.go" Maybe you can find a way to extract the project root? |
||
| } else { | ||
| file = frame.File | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the tests are failing with the current solution, please run them locally before submitting next time :) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,63 @@ import ( | |
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestLogCallerTrimmer(t *testing.T) { | ||
| t.Run("dir name is just lakeFS", func(t *testing.T) { | ||
| frame := &runtime.Frame{ | ||
| File: "lakefs/to/main.go", | ||
| Line: 42, | ||
| Function: "main.someFunction", | ||
| } | ||
| wantFile := "lakefs/to/main.go:42" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case actually it should be: |
||
| wantFunction := "main.someFunction" | ||
| resultFunction, resultFile := logCallerTrimmer(frame) | ||
| if resultFile != wantFile { | ||
| t.Fatalf("Result File '%s', should be '%s'", resultFile, wantFile) | ||
| } | ||
| if resultFunction != wantFunction { | ||
| t.Fatalf("Result Function '%s', should be '%s'", resultFunction, wantFunction) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("dir name is lakeFS-foo", func(t *testing.T) { | ||
| frame := &runtime.Frame{ | ||
| File: "lakefs-foo/to/main.go", | ||
| Line: 42, | ||
| Function: "main.someFunction", | ||
| } | ||
| wantFile := "lakefs-foo/to/main.go:42" | ||
| wantFunction := "main.someFunction" | ||
| resultFunction, resultFile := logCallerTrimmer(frame) | ||
| if resultFile != wantFile { | ||
| t.Fatalf("Result File '%s', should be '%s'", resultFile, wantFile) | ||
| } | ||
| if resultFunction != wantFunction { | ||
| t.Fatalf("Result Function '%s', should be '%s'", resultFunction, wantFunction) | ||
| } | ||
| }) | ||
| t.Run("dir name is just full absolute path", func(t *testing.T) { | ||
| frame := &runtime.Frame{ | ||
| File: "Users/Apps/lakeFS-foo/to/main.go", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if this is even a legit case |
||
| Line: 42, | ||
| Function: "main.someFunction", | ||
| } | ||
| wantFile := "lakeFS-foo/to/main.go:42" | ||
| wantFunction := "main.someFunction" | ||
| resultFunction, resultFile := logCallerTrimmer(frame) | ||
| if resultFile != wantFile { | ||
| t.Fatalf("Result File '%s', should be '%s'", resultFile, wantFile) | ||
| } | ||
| if resultFunction != wantFunction { | ||
| t.Fatalf("Result Function '%s', should be '%s'", resultFunction, wantFunction) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestSetOutputs(t *testing.T) { | ||
| t.Run("default", func(t *testing.T) { | ||
| currentOut := defaultLogger.Out | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was the "reflect" library deleted? We need this lib :)