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.catch to log file?

See original GitHub issue

I had a look through other issues and couldn’t find anything relevant so here goes.

Can I intercept an unhandled exception in my function so that the printed traceback gets also put into my sink file? I tried with both @logger.catch() and @logger.catch(reraise=True), both to no avail.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
Delgancommented, Dec 8, 2019

Exceptions occurring inside a function decorated with @logger.catch should be logged to your sinks.

Basically, logger.catch() is a s simple wrapper around your code which does the following:

try:
    return some_function()
except Exception:
    logger.exception("An error occurred")

You may need to wrap your entire application of you want to catch any exception occurring anywhere, for example (logger.catch() can be used as a context manager too):

if __name__ == "__main__":
    with logger.catch():
        start_my_app()

The stack trace should be written to your file as long as the exception is caught by Loguru, or that you manually log it using logger.exception("Some message") in the except block of exception handling.

0reactions
Delgancommented, Mar 4, 2022

@mowarsadsa The test function does not seem decorated with logger.catch() so the raised Exception can’t be captured by Loguru.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logger - loguru documentation - Read the Docs
When a message is logged, a “record” is associated with it. This record is a dict which contains information about the logging context:...
Read more >
How to get file the Python logging module is currently logging ...
For my basic usage of a single file log, this worked logging.getLoggerClass().root.handlers[0].baseFilename.
Read more >
Logging Exceptions in Java - Loggly
In our program, we have a class that opens a file, writes to it, and then closes it. In this class, we catch...
Read more >
Exception Logging to Text file - C# Corner
In this article we will learn about the Exception Logging to Text file using asp.net C#.
Read more >
How to log a Python exception? - GeeksforGeeks
Logging an exception in python with an error can be done in the logging.exception() method. This function logs a message with level ERROR...
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