Adding python logging to fastapi endpoints hosted on docker doesnt display api

When developing a FastAPI application hosted on Docker, it is important to have proper logging in place to monitor the application’s behavior and troubleshoot any issues that may arise. However, sometimes the logging output may not be displayed when accessing the API endpoints. In this article, we will explore three different ways to solve this problem and determine which option is the most effective.

Option 1: Configuring logging in FastAPI

The first option is to configure logging directly in the FastAPI application. This can be done by adding the following code at the beginning of the Python script:

import logging


This code sets the logging level to DEBUG, which means that all log messages with a severity level of DEBUG or higher will be displayed. By default, FastAPI uses the Uvicorn server, which also has its own logging configuration. To ensure that the logging output is displayed, we can add the following code:

import uvicorn

if __name__ == "__main__":, host="", port=8000, log_level="debug")

This code sets the log_level parameter to “debug”, which enables the display of DEBUG level log messages.

Option 2: Configuring logging in Docker

If the logging output is still not displayed after configuring logging in FastAPI, the next option is to configure logging in the Docker container. This can be done by modifying the Dockerfile used to build the container. Add the following lines to the Dockerfile:

FROM python:3.9

# Install dependencies

# Set the logging level

# Run the application
CMD ["python", ""]

This code sets the LOG_LEVEL environment variable to DEBUG, which will be used by the FastAPI application to configure the logging level. By default, the logging level is set to WARNING, so setting it to DEBUG ensures that all log messages will be displayed.

Option 3: Using a logging configuration file

If the previous options did not solve the issue, the final option is to use a logging configuration file. This allows for more advanced logging configurations and customization. Create a file named “logging.conf” with the following content:






format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

This configuration file sets the logging level to DEBUG and configures a StreamHandler to display the log messages on the console. To use this configuration file in the FastAPI application, add the following code at the beginning of the Python script:

import logging.config


This code loads the logging configuration from the “logging.conf” file and applies it to the application.

After exploring these three options, it is clear that the best solution depends on the specific requirements and preferences of the project. Option 1 is the simplest and most straightforward, as it configures logging directly in the FastAPI application. However, if more advanced logging configurations are needed, options 2 and 3 provide more flexibility. Option 2 allows for easy configuration of the logging level in the Docker container, while option 3 allows for more advanced customization using a logging configuration file.

In conclusion, the best option for adding Python logging to FastAPI endpoints hosted on Docker depends on the specific needs of the project. For simple logging requirements, option 1 is recommended. For more advanced configurations, options 2 and 3 provide additional flexibility.

Rate this post

9 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents