Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io"
"os"
"reflect"
"regexp"
Copy link
Contributor

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 :)

"runtime"
"slices"
"strings"
Expand Down Expand Up @@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this solution doesn’t achieve the desired result.
Note that logCallerTrimmer is intended to trim caller paths to be relative to the project root.

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.
A return value of nil indicates no match.

(See an example in the docs)

Applying this to our case:

For the path:
"/Users/annaseliverstov/work/lakefs-anna/pkg/block/factory/build.go"
The matches slice will contain:

matches[0] == "/Users/annaseliverstov/work/lakefs-anna/pkg/block/factory/build.go"
matches[1] == "lakefs-anna"

So the resulting file value would be:
file = "lakefs-anna"

When the expected result should be:
file = "pkg/block/factory/build.go"

Please make sure your solution also correctly handles paths like:

"/Users/annaseliverstov/work/lakefs-anna/cmd/lakefs/cmd/run.go"
"/Users/annaseliverstov/work/lakefs-anna/lakefs-anna/pkg/block/factory/build.go"

Maybe you can find a way to extract the project root?

} else {
file = frame.File
}
Expand Down
53 changes: 53 additions & 0 deletions pkg/logging/logger_test.go
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case actually it should be: wantFile := "to/main.go:42" if assuming the lakefs in the path is the project root

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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down