console-slog icon indicating copy to clipboard operation
console-slog copied to clipboard

Better trimming of the source file path

Open ansel1 opened this issue 1 year ago • 4 comments

Only make the source file path relative to the current working directly if the source file is a child path of the current working directory.

Typically, the source file path will only be a child of the CWD when the code is being run from its own project folder. But if the executable moved somewhere else first, or packaged in a container, or compiled with the -trimpath option, then this relative path logic doesn't work.

If the file path isn't related to the CWT, then trim to just one path element above the file name, which is typically the go package name, e.g. "io/reader.go"

Address #16

ansel1 avatar Jan 17 '25 20:01 ansel1

Hi ! Thank you for this improvement. What about deeply nested go packages, like foo/bar/baz ? Only bar/baz will be displayed right ?

phsym avatar Jan 20 '25 15:01 phsym

After thinking about it, I feel this can be quite inconvenient, and it may drop the module name as many go modules could be using slog together.

Just an idea, but maybe there's something to do with debug.ReadBuildInfo()

phsym avatar Jan 20 '25 16:01 phsym

Certainly another option would be to show the full path if that full path is not a child of the current working directory. In my experimentation, I found that the file path could either be:

  1. an actual file path, which typically looks like "/Users/me/projects/mygomod/package/package/file.go"
  2. an SDK go module, like io/reader.go
  3. if the -trimpath option is used with the compiler, then the "path" becomes the import path, like "github.com/me/mygomod/package/package/file.go"

If you just print out the full path though, these values get really long and end up taking over a big chunk of the log line. For me, I preferred the look of the truncated line.

I'm playing now with truncating the source and the other headers ever further to guarantee (optionally) a fixed size header block, so the actual message always starts on the same column. For the source field, I'm thinking of truncating by dropping characters out of the middle of the path components, sort of like how fish shell shortens path names in the prompt.

So like:

github.com/ansel1/merry/core/err.go:56

might shorten to:

ans1/mry/cor/err:56

So like, drop the module host, vowels, repeated characters, the .go extension...still playing with it to see how it looks.

ansel1 avatar Jan 20 '25 20:01 ansel1

Oh, also, I don't have a windows machine to generate sample values for a test. Do you have any samples?

ansel1 avatar Jan 22 '25 19:01 ansel1