When I execute logger.info(traceback.print_exc())
the trace gets on console rather than in the log file
I have logger.propagate = False
also still the same issue
import traceback traceback. format_exc() # this will print a complete trace to stout. Save this answer.
In Python, A traceback is a report containing the function calls made in your code at a specific point i.e when you get an error it is recommended that you should trace it backward(traceback). Whenever the code gets an exception, the traceback will give the information about what went wrong in the code.
To log an exception in Python we can use logging module and through that we can log the error. Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger.
print_exc
prints the stack trace to stderr.
Just use the exc_info=1 argument and it will automaticaly include the exception.
logging.exception("Exception") #or
logging.error("exception ",exc_info=1) #or
logging.info("Exception has occured" ,exc_info=1)
I am using python 2.7 and sadly exc_info=1
never worked for me so I had to use this:
import traceback
...
log.debug(traceback.format_exc())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With