Logger format for relative path to file where log was made and logging package logs to different files.
See original GitHub issueI’ve always found that an extremely useful feature for debugging is having the relative path to the file where the log was made in the log. This allows you to control click the path:line and be immediately brought to the log in vscode (not sure how it works in other ide/editors).
Example of this, we have the structure
setup.py
test_app
|_test_folder
|_app.py
where app.py has a logger that calls logger.error('Invalid state')
on line 50
If the log has the following: [test_app/test_folder/app.py:50] Invalid State
and you control click the path you will taken to app.py:50
Using {name} prints the path with . instead of / and I am unsure if there is a way to convert the . to / (which would very adequately resolve the issue) and {file.path} prints an absolute path to the file which also allows for the control click functionality BUT makes the log very long in most cases (think /home/user/workspace/app_root/test_app/test_folder/app.py:50) or even worse if its a package in a virtual environment making the call.
In addition, I think I would have really benefitted from a complete list of formatter options for Loguru that are immediately available and easily findable. Perhaps this was a me issue, but I struggle quite a lot to find https://loguru.readthedocs.io/en/stable/api/logger.html#record which I am still unsure if its complete.
Last thing, I would love to be able to separate my app logs from logs made by packages (like werkzeug completely overrunning my logs) into different files. Given that the point of loguru is to have one formatter, I have a feeling this won’t really be possible, but thought I’d ask anyway.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
@prchristie Yeah, I would like to avoid special cases where the log messages comes from a library which isn’t located in the package itself (might be in
src
or in the virtual environment).By the way, until a possible solution is implemented in the next release, you can use
patch()
as a workaround:@wjzhou Thanks for the feedback. You can configure your handler with
"{file.path}"
in theformat
to get the absolute path.You can also set the
LOGURU_FORMAT
environment variable to your preferredformat
and it will be used by default. 👍Thank you for the suggestion. works perfectly for my needs.
FYI, for anyone searched this issue. Please use:
logger.add(sys.stderr, format="{file.path}:{line} {message}")
And Ctrl-Click on the file will open the line in vscode.