r/FastAPI • u/Successful-Moment594 • Mar 11 '21
Question Not getting exception traceback
In am trying to learn fastapi, I am creating a basic app using fastapi,sqlalchemy,alembic. But whenever there is a problem I am using :
try:
suspected_line_with_problem
except Exception as e:
print(e)but this is very much time consuming and I am sure this is not the right way to debug errors. I have also tried these lines to log the errors:
#in main.py
log = logging.getLogger("uvicorn")
log.setLevel(logging.DEBUG)
command_line_handler = logging.StreamHandler()
command_line_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)s- api - %(asctime)s - %(message)s')
command_line_handler.setFormatter(formatter)
log.addHandler(command_line_handler)Still I am not getting trackback or source of error, I have tried to start uvicorn with parameter --reload --log-level debug but still I am not seeing any traceback/helpful message, All I am getting is:
←[32mINFO←[0m: 127.0.0.1:62571 - "GET /blog/id/?num=4 HTTP/1.1" 500Please guide.
1
u/OpposablePinky Mar 12 '21
logging.getLogger("myapp").exception ("some message that will log with the traceback")
1
0
2
u/iamtheconundrum Mar 11 '21
Don’t use the except this way if you want to debug the code. You’ll miss valuable context making it harder to find the root of the error.