4

Put simply, I have a piece of code that looks like this:

if some_condition_that_evals_to_True:
    raise ValueError("Error message")

What I want to do is to insert a logging statement that logs this entire exception but doing it just this way only saves the logger message:

if some_condition_that_evals_to_True:
    logger.error("Logged error message")
    raise ValueError("Error message")

Any one know how to save the entire error message including the ValueError?

EDIT:

The following is what I am trying to recreate:

if some_condition_that_evals_to_True:
    try:
        raise ValueError("Error with value")
    except ValueError:
        logger.exception("Logging Error with Value")
        raise 

But this seems like a roundabout way to get the behavior I want, so another way to phrase my question: Is there a more elegant way get the same behavior as the above codeblock?

7
  • 2
    What actually happened when you tried this? Show the actual output, and how it differs from what you wanted. In short, don't ask us whether it will work, ask the ultimate authority: your Python run-time system. :-) Commented May 18, 2017 at 23:22
  • @Prune Tried it and got the expected result and I edited the description to reflect that Commented May 18, 2017 at 23:54
  • 1
    Great. Try an exception handler for ValueError and log what you like in that. Will that work for you? Commented May 18, 2017 at 23:56
  • @Prune Are you saying insert a try-except on the inside of the if statement with just the raised error and catch the exception that way? Commented May 19, 2017 at 0:04
  • No, I was suggesting that you put the handler in the routine that calls this one. If it were as simple as adding a line or to in the current if I trust that you would have already tried that. Commented May 19, 2017 at 0:16

1 Answer 1

4

Try the stack_info keyword argument when using the logging module:

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
if True:
    logging.error('Error Message', stack_info=True)
    raise ValueError('Custom Error Message')

Running this shows the following:

J:\>python log_stack.py
Traceback (most recent call last):
  File "log_stack.py", line 5, in <module>
    raise ValueError('Custom Error Message')
ValueError: Custom Error Message

J:\>more example.log
ERROR:root:Error Message
Stack (most recent call last):
  File "log_stack.py", line 4, in <module>
    logging.error('Error Message', stack_info=True)
Sign up to request clarification or add additional context in comments.

3 Comments

This is almost exactly what I was hoping for. Is there a way log that the the ValueError is what caused the error? Essentially just log what is printed
@Abdullah The ValueError didn't cause the error, ValueError is merely is mechanism to inform other code of the specific error type. Your log message should be informative for humans, so a log message like 'Invalid value %r for foo' % foo should give you all the information you need.
@Abdullah you are able to instantiate an exception, e.g. e = ValueError('custom') and then log that using logging.exception(e, stack_info=True), however the documentation says that should only be called from an exception handler (I don't know the specific reasons), which is why I haven't used it above. It works in our trivial example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.