Top Solutions for FastAPI Logging: Best Practices and Implementation

Jennie Lee
5 min readMar 14, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction to FastAPI and its advantages

FastAPI is a Python web framework that is gaining popularity among developers for building REST APIs. It is known for its speed, simplicity, and ease of use. What sets FastAPI apart from other frameworks is its ability to automatically generate OpenAPI documentation, making it easier for developers to document their APIs without any manual effort.

For my latest project, I evaluated different Python web frameworks and decided to use FastAPI due to its powerful features and advantages. The automatic generation of OpenAPI documentation was a major factor in my decision, as it saves time and effort when documenting API endpoints and models.

Overview of Google Cloud Logging

Since my project is running on Google Cloud Platform (GCP), I wanted to ensure that the logging of activities is clean and traceable. That’s when I came across Google Cloud Logging, a logging and tracing solution provided by Google.

Google Cloud Logging allows you to collect log entries from various sources in a central location. This makes it easier to monitor and manage logs from multiple services or applications. Additionally, Google Cloud Logging provides a custom query language that allows you to filter and retrieve log entries based on specific criteria.

The benefits of using Google Cloud Logging are quite evident. Firstly, it simplifies log management by providing a centralized location for log collection and retrieval. Secondly, the custom query language allows you to search and analyze log entries efficiently. Finally, it seamlessly integrates with other Google Cloud services, making it an ideal choice for projects running on Google Cloud.

Enabling feature-rich logging for FastAPI with Google Cloud Logging

While Google Cloud Logging offers an extensive set of features and integrations, native support for FastAPI is currently not available. To overcome this limitation, I had to implement custom middleware and logging filters to enable feature-rich logging for my FastAPI application.

Implementing a middleware class for gathering request information

The first step in implementing feature-rich logging for FastAPI is to create a middleware class that gathers information about each incoming request. This information can include the request method, path, headers, and any other relevant details.

By gathering this information, we can provide context to the log entries and make them more meaningful. For example, we can log the request method and path, along with any custom headers that contain trace information.

To implement the middleware class, follow these steps:

  1. Create a new file called middleware.py in your FastAPI project directory.
  2. Import the necessary modules and classes:
from fastapi import Request
from starlette.middleware.base import BaseHTTPMiddleware
  1. Create a new class that extends BaseHTTPMiddleware:
class LoggingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Gather request information here

response = await call_next(request)
return response
  1. Implement the logic to gather request information. For example, you can log the request method, path, headers, and any other relevant details. You can store this information in context variables or any other suitable data structure.
class LoggingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Gather request information
method = request.method
path = request.url.path
headers = dict(request.headers)

# Store information in context variables
request.scope["method"] = method
request.scope["path"] = path
request.scope["headers"] = headers

response = await call_next(request)
return response
  1. Finally, add the middleware to your FastAPI application by adding the following lines of code:
from fastapi import FastAPI
from .middleware import LoggingMiddleware

app = FastAPI()

app.add_middleware(LoggingMiddleware)

By implementing this middleware class, you can gather essential information about each incoming request and store it in context variables. This information will be used by the logging filter to append the necessary details to each log entry.

Implementing a logging filter for appending necessary information

The middleware class allows us to gather request information, but we still need to append that information to each log entry. To achieve this, we need to implement a logging filter that extends the CloudLoggingFilter provided by the Google Cloud Logging library.

The logging filter is responsible for appending trace information, such as trace ID and span ID, to each log entry. This allows us to track the flow of requests and gather more context about the activities happening within our FastAPI application.

To implement the logging filter, follow these steps:

  1. Create a new file called logging.py in your FastAPI project directory.
  2. Import the necessary modules and classes:
import logging
from google.cloud.logging_v2.handlers import CloudLoggingHandler, setup_logging
from google.cloud.logging_v2.handlers.cloud_logging import CloudLoggingFilter
  1. Create a new class that extends the CloudLoggingFilter:
class CustomLoggingFilter(CloudLoggingFilter):
def __init__(self):
super().__init__()

def filter(self, record):
# Append necessary information to log entries
method = getattr(record, "method", None)
path = getattr(record, "path", None)
headers = getattr(record, "headers", None)

record.method = method
record.path = path
record.headers = headers

return super().filter(record)
  1. Implement the logic to append the necessary information to each log entry. You can access the request information stored in context variables and add it to the log record.
class CustomLoggingFilter(CloudLoggingFilter):
def __init__(self):
super().__init__()

def filter(self, record):
# Append necessary information to log entries
method = getattr(record, "method", None)
path = getattr(record, "path", None)
headers = getattr(record, "headers", None)

record.method = method
record.path = path
record.headers = headers

return super().filter(record)
  1. Finally, configure the Python logger to use the desired logging logic by adding the following lines of code:
google_cloud_handler = CloudLoggingHandler()
google_cloud_handler.addFilter(CustomLoggingFilter())

# Configure the root logger
logger = logging.getLogger()
logger.addHandler(google_cloud_handler)
setup_logging()

By implementing this logging filter, you can append the necessary information to each log entry, including the request method, path, and headers. This ensures that the log entries in Google Cloud Logging contain the required context for better traceability.

File structure and setup for logging implementation

To successfully implement feature-rich logging for FastAPI, you need to ensure that your file structure and setup are configured correctly. Here’s a recommended file structure for your FastAPI project:

.
├── main.py
├── middleware.py
└── logging.py

In the main.py file, you have your FastAPI application code. This is where you add the middleware and configure the logger. Here's an example of how the main.py file could look like:

from fastapi import FastAPI
from .middleware import LoggingMiddleware

app = FastAPI()

app.add_middleware(LoggingMiddleware)

# Configure the logger
google_cloud_handler = CloudLoggingHandler()
google_cloud_handler.addFilter(CustomLoggingFilter())

# Configure the root logger
logger = logging.getLogger()
logger.addHandler(google_cloud_handler)
setup_logging()

@app.get("/")
def root():
return {"message": "Hello, World!"}

In the middleware.py file, you have the implementation of the middleware class that gathers request information.

In the logging.py file, you have the implementation of the logging filter that appends the necessary information to each log entry.

By following this file structure and setup, you can organize your code and ensure that the logging implementation works as expected.

In conclusion, enabling feature-rich logging for FastAPI on Google Cloud Logging requires implementing custom middleware and logging filters. Although FastAPI does not have built-in support for Google Cloud Logging, it is relatively easy to implement the necessary functionality. By gathering request information using a middleware class and appending the necessary details using a logging filter, you can achieve feature-rich logging for your FastAPI application. This enables better traceability and monitoring of activities, making it easier to debug and analyze your application’s behavior.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--

Jennie Lee
Jennie Lee

Written by Jennie Lee

Software Testing Blogger, #API Testing

No responses yet