Skip to content
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

File path for source field is calculated from CWD, which only works in original project folder #16

Open
ansel1 opened this issue Jan 17, 2025 · 0 comments

Comments

@ansel1
Copy link

ansel1 commented Jan 17, 2025

If AddSource=true, the source field in the log is a relative file name, relative to the current working directory. That does work ok when this handler is used in code being run from a go project, as the go source files are typically relative to the project's root folder, and that's typically also going to be the CWD when the project is run.

But if the executable is run from a different location (like if it is packaged in a docker container), when the working directory is going to be unrelated to the source file location. You end up with log entries like:

../../../../Users/regan/dev/sallyportflumev2/sallyport/cmd/sallyport/main.go:49

What would be ideal is to always calculate the relative path against the root of the go module that particular source file originally came from, but I'm not sure that's possible.

I don't have a suggestion for an alternative yet. I'll think about it. I think other projects just show the filename with no path.

ansel1 added a commit to ansel1/console-slog that referenced this issue Jan 17, 2025
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 phsym#16
ansel1 added a commit to ansel1/console-slog that referenced this issue Jan 17, 2025
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"

Addresses phsym#16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant