-
Notifications
You must be signed in to change notification settings - Fork 715
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
some .opt(exception=...) quirks and trying to log exception without sys.exc_info #1284
Comments
Hey @karlicoss, thank you for sharing your detailed thoughts. I hadn’t realized that However, I don’t think passing exceptions directly to the logging method is an ideal approach. I feel that the primary argument of logging method should be the log message as a string, while other type arguments such as the exception should be provided as additional context. That’s why I think it's preferable to pass the exception as an argument of That being said, it might make sense for the exception in On the other hand, introducing this precedence would add some logical complexity. The behavior of Personally, I would rather lean toward improving the documentation for |
Ah thanks for explaining!
I feel like it's the whole point of .opt though! It contains a lot of parameters that define the behaviour of logging :) I guess my view is that I'd rather make exception logging as effortless as possible -- otherwise remembering to use |
Hi! I've been trying to figure out how to dump stack trace on exception in loguru by default
Consider this snippet:
this works exactly as expected and is logging the stack trace.
However, in my code I often have some sort of defensive logic -- I execute a bunch of operations, catch & collect the Exceptions and then do something else with them downstream, i.e. logging might not happen in the
except
clause.Simplified, something like this:
Now we get this
This is because at the point of
logger.exception
call,sys.exc_info is None
(we're outside ofexcept
clause).This behaviour by default perhaps makes some sense -- e.g. it's consistent with builtin
logging.exception
So next thing I tried is using
opt
:... which to my surprise also resulted in:
This is because
Logger.exception
function unconditionally setsoptions[0]
toTrue
, overwriting an actual Exception object/tuple.loguru/loguru/_logger.py
Lines 2098 to 2101 in 8653835
Funny enough, if we use anything except
.exception
to log, it works as expected and prints the trace (e.g.logger.opt(exception=ex).debug(ex)
).IMO it doesn't make much sense that .exception logs less exception information than other functions!
Shouldn't it at least check first and only set
options[0]
toTrue
if the previous value was None or False?suggestion to opt-in to use exception's exception info
Now, even if that was working correctly, it's a bit mouthful to write
.opt(exception=ex).exception(ex)
every time when you're trying to log an exception.I think it would be nice if there was possible to do something like that:
, i.e. make
.exception
calls automatically use the exception they are logging for the traceback information.'auto'
is just the first thing I came up with, open to a better name suggestion!Do you think it would make sense? I'm happy to try to implement this along with some tests.
The text was updated successfully, but these errors were encountered: