question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logger format for relative path to file where log was made and logging package logs to different files.

See original GitHub issue

I’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:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Delgancommented, Jun 1, 2022

Alternatively or additionally some solution where we actually replace . with / in the module name then its always relative to the package module layout, but this might have issue with projects that use a src folder as their top level instead of package name.

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

def add_relative_path(record):
    record["extra"]["relative_path"] = record["name"].replace(".", "/") + ".py"

logger.configure(patcher=add_relative_path)

logger.add(sys.stderr, format="{extra[relative_path]}:{line} {message}")

For my use case, I think add an option/(env variable) to output absolute path is sufficient.

@wjzhou Thanks for the feedback. You can configure your handler with "{file.path}"in the format to get the absolute path.

logger.add(sys.stderr, format="{file.path}:{line} {message}")

You can also set the LOGURU_FORMAT environment variable to your preferred format and it will be used by default. 👍

0reactions
wjzhoucommented, Jun 1, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Relative Paths To Log Files In Pylons' development.ini
The problem is that Paste Deploy creates one ConfigParser object to store the 'here' tag in it's set of defaults, and logging.config.
Read more >
Logging HOWTO — Python 3.11.1 documentation
The root logger's name is printed as 'root' in the logged output. It is, of course, possible to log messages to different destinations....
Read more >
Python Logging - Saving Logs to a File & Sending Logs to an ...
This article shows you two ways of saving your logs using handlers. The first is the simplest; just writing all logs to a...
Read more >
Apache Logging Basics - The Ultimate Guide To Logging
We'll also cover advanced topics such as setting custom log formats and ... By default, Apache stores access and error logs in separate...
Read more >
Python Logging Guide - Best Practices and Hands-on Examples
Handlers send the log records (created by loggers) to the appropriate ... code the read CSV files and other files with a similar...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found