FastAPI Logging in 2023

File logging, database logging, console logging?

Mike Wolfe
CodeX

--

Photo by Sebastian Pociecha on Unsplash

In the world of back-end software development, proper logging is an integral part of any Application Programming Interface (API). Having the ability to see what is happening at all times with your API makes it easier to debug issues, see how your clients are interacting with the API and keep an eye on the general health of your website. So, today we are going to take a look at a few ways to implement logging in FastAPI.

Console Logging

By far, the easiest type of logging to implement is console logging. In it’s simplest form, when using Python, all you have to do is this:

print("Some log message.")

Now this may seem all fine and dandy, but behind the scenes, all that this print function does is print the string to standard out (stdout). For proper logging, you are going to want something a little more robust. Something that will properly utilize all the different logging levels, Debug, Critical, Error, Info, etc. Luckily, there is something that can fit these needs and that is the Python logging module. This module is part of Python’s standard library, but if for whatever reason it happens to be missing from your development environment it can be installed using pip.

pip install logging

--

--

Mike Wolfe
CodeX
Writer for

Software Developer, Tech Enthusiast, Runner. Current project http://sqlcheater.com/ Connect with me on LinkedIn: www.linkedin.com/in/michael-wolfe-176212125