fastapi-gae-logging 0.0.6__tar.gz → 0.0.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,21 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fastapi-gae-logging
3
- Version: 0.0.6
4
- Summary: Custom Cloud Logging handler for FastAPI applications deployed in Google App Engine. Groups logs coming from the same request lifecycle and propagates the maximum log level throughout the request lifecycle using middleware and context management.
3
+ Version: 0.0.8
4
+ Summary: Custom Cloud Logging handler for FastAPI (or any Starlette based) applications deployed in Google App Engine. Groups logs coming from the same request lifecycle and propagates the maximum log level throughout the request lifecycle using middleware and context management.
5
5
  Home-page: https://github.com/chrisK824/fastapi-gae-logging
6
6
  Author: Chris Karvouniaris
7
7
  Author-email: christos.karvouniaris247@gmail.com
8
8
  License: MIT
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Operating System :: OS Independent
11
- Requires-Python: >=3.7
11
+ Requires-Python: >=3.8
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
- Requires-Dist: fastapi>=0.112.2
15
- Requires-Dist: google-cloud-logging>=3.11.2
14
+ Requires-Dist: starlette
15
+ Requires-Dist: google-cloud-logging
16
16
 
17
17
  # fastapi-gae-logging
18
- Custom Cloud Logging handler for FastAPI applications deployed in Google App Engine to ease out logs analysis and monitoring through Google Cloud Log Explorer.
18
+ Custom Cloud Logging handler for FastAPI (or any Starlette based) applications deployed in Google App Engine to ease out logs analysis and monitoring through Google Cloud Log Explorer.
19
19
 
20
20
  ## What problem does this package solve? Why do we need this?
21
21
  When deploying FastAPI applications on Google App Engine, I encountered several challenges with logging and observability even when using the official package for this purpose, `google-cloud-logging`.
@@ -57,7 +57,7 @@ The `fastapi-gae-logging` module addresses these problems by:
57
57
 
58
58
  ```python
59
59
  FastAPIGAELoggingHandler(
60
- app: FastAPI,
60
+ app: Starlette,
61
61
  request_logger_name: Optional[str] = None,
62
62
  log_payload: bool = True,
63
63
  *args, **kwargs
@@ -65,7 +65,7 @@ FastAPIGAELoggingHandler(
65
65
  ```
66
66
 
67
67
  - Parameters
68
- - **app** (FastAPI): The FastAPI application instance.
68
+ - **app** (FastAPI | Starlette): The FastAPI or Starlette application instance.
69
69
  - **request_logger_name** (Optional[str], optional): The name of the Cloud Logging logger to use for request logs.
70
70
  Defaults to the Google Cloud Project ID with the suffix '-request-logger'.
71
71
 
@@ -166,7 +166,7 @@ def post_payload(payload: Any = Body(None)):
166
166
  ## Dependencies
167
167
  This tool is built upon the following packages:
168
168
 
169
- - `fastapi`: A modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
169
+ - `starlette`: A modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
170
170
  - `google-cloud-logging`: Google Cloud Logging API client library for logging and managing logs in Google Cloud Platform.
171
171
 
172
172
 
@@ -1,5 +1,5 @@
1
1
  # fastapi-gae-logging
2
- Custom Cloud Logging handler for FastAPI applications deployed in Google App Engine to ease out logs analysis and monitoring through Google Cloud Log Explorer.
2
+ Custom Cloud Logging handler for FastAPI (or any Starlette based) applications deployed in Google App Engine to ease out logs analysis and monitoring through Google Cloud Log Explorer.
3
3
 
4
4
  ## What problem does this package solve? Why do we need this?
5
5
  When deploying FastAPI applications on Google App Engine, I encountered several challenges with logging and observability even when using the official package for this purpose, `google-cloud-logging`.
@@ -41,7 +41,7 @@ The `fastapi-gae-logging` module addresses these problems by:
41
41
 
42
42
  ```python
43
43
  FastAPIGAELoggingHandler(
44
- app: FastAPI,
44
+ app: Starlette,
45
45
  request_logger_name: Optional[str] = None,
46
46
  log_payload: bool = True,
47
47
  *args, **kwargs
@@ -49,7 +49,7 @@ FastAPIGAELoggingHandler(
49
49
  ```
50
50
 
51
51
  - Parameters
52
- - **app** (FastAPI): The FastAPI application instance.
52
+ - **app** (FastAPI | Starlette): The FastAPI or Starlette application instance.
53
53
  - **request_logger_name** (Optional[str], optional): The name of the Cloud Logging logger to use for request logs.
54
54
  Defaults to the Google Cloud Project ID with the suffix '-request-logger'.
55
55
 
@@ -150,7 +150,7 @@ def post_payload(payload: Any = Body(None)):
150
150
  ## Dependencies
151
151
  This tool is built upon the following packages:
152
152
 
153
- - `fastapi`: A modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
153
+ - `starlette`: A modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
154
154
  - `google-cloud-logging`: Google Cloud Logging API client library for logging and managing logs in Google Cloud Platform.
155
155
 
156
156
 
@@ -5,7 +5,7 @@ import os
5
5
  import re
6
6
  import json
7
7
  from typing import Optional, Dict, Any
8
- from fastapi import FastAPI
8
+ from starlette.applications import Starlette
9
9
  from starlette.exceptions import HTTPException
10
10
  from starlette.types import ASGIApp, Receive, Scope, Send
11
11
  from starlette.responses import Response
@@ -56,6 +56,20 @@ class LogInterceptor(logging.Filter):
56
56
 
57
57
 
58
58
  class GAERequestLogger:
59
+ """
60
+ A logger for emitting structured request logs in Google App Engine.
61
+
62
+ This logger is designed to work with FastAPI applications deployed on Google App Engine. It logs
63
+ structured data after each request is handled, including the HTTP request method, URL, status,
64
+ user agent, response size, latency, and remote IP address. The log severity is determined by the
65
+ maximum log level recorded during the request.
66
+
67
+ Attributes:
68
+ LOG_LEVEL_TO_SEVERITY (Dict[int, str]): Mapping of Python logging levels to Cloud Logging severity levels.
69
+ logger (Logger): The Google Cloud Logger instance to log requests.
70
+ resource (Resource): The Google Cloud resource associated with the logger.
71
+ log_payload (bool): Whether to log the request payload for certain HTTP methods. Defaults to True.
72
+ """
59
73
  LOG_LEVEL_TO_SEVERITY: Dict[int, str] = {
60
74
  logging.NOTSET: 'DEFAULT',
61
75
  logging.DEBUG: 'DEBUG',
@@ -72,6 +86,7 @@ class GAERequestLogger:
72
86
  Args:
73
87
  logger (Logger): The Google Cloud Logger instance to log requests.
74
88
  resource (Resource): The resource associated with the logger.
89
+ log_payload (bool): Whether to log the request payload for certain HTTP methods. Defaults to True.
75
90
  """
76
91
  self.logger = logger
77
92
  self.resource = resource
@@ -143,15 +158,41 @@ class GAERequestLogger:
143
158
 
144
159
  class FastAPIGAELoggingMiddleware:
145
160
  """
146
- Custom middleware to set up request start time and emit logs after request completion.
147
- Specifically designed for FastAPI applications deployed on Google App Engine.
161
+ Middleware to set up request start time and emit logs after request completion.
162
+
163
+ This middleware is specifically designed for FastAPI applications deployed on Google App Engine.
164
+ It records the start time of the request and then emits a log after the request is completed,
165
+ containing details about the request and response.
166
+
167
+ Attributes:
168
+ app (ASGIApp): The ASGI application instance.
169
+ logger (GAERequestLogger): The logger used to emit structured logs.
148
170
  """
149
171
 
150
172
  def __init__(self, app: ASGIApp, logger: GAERequestLogger):
173
+ """
174
+ Initialize the middleware.
175
+
176
+ Args:
177
+ app (ASGIApp): The ASGI application instance.
178
+ logger (GAERequestLogger): The logger instance used to log request data.
179
+ """
151
180
  self.app = app
152
181
  self.logger = logger
153
182
 
154
183
  async def __call__(self, scope: Scope, receive: Receive, send: Send):
184
+ """
185
+ Middleware entry point, invoked for each request.
186
+
187
+ This method sets up the request context with trace information, start time, and initial log level.
188
+ It then intercepts the response to capture details for logging. In case of exceptions, they are
189
+ logged, and the exception is re-raised.
190
+
191
+ Args:
192
+ scope (Scope): The ASGI scope dictionary containing request information.
193
+ receive (Receive): The receive channel for incoming messages.
194
+ send (Send): The send channel for outgoing messages.
195
+ """
155
196
  if scope["type"] == "http":
156
197
 
157
198
  # https://stackoverflow.com/questions/64115628/get-starlette-request-body-in-the-middleware-context
@@ -171,10 +212,10 @@ class FastAPIGAELoggingMiddleware:
171
212
  'max_log_level': logging.NOTSET
172
213
  })
173
214
 
174
- # start with the default response in case of generic error
215
+ # Default response in case of an error
175
216
  response = Response(status_code=500)
176
217
 
177
- # intercept the sent message to get the response status and body
218
+ # Intercept the sent message to get the response status and body
178
219
  # for later use
179
220
  async def send_spoof_wrapper(message: Dict[str, Any]) -> None:
180
221
  if message["type"] == "http.response.start":
@@ -195,16 +236,18 @@ class FastAPIGAELoggingMiddleware:
195
236
 
196
237
  class FastAPIGAELoggingHandler(CloudLoggingHandler):
197
238
  """
198
- Custom Cloud Logging handler for FastAPI applications deployed in Google App Engine.
199
- Groups logs coming from the same request lifecycle and propagates the maximum log level
200
- throughout the request lifecycle using middleware and context management.
239
+ Custom Cloud Logging handler for FastAPI applications deployed on Google App Engine.
240
+
241
+ This handler groups logs from the same request lifecycle and propagates the maximum log level
242
+ observed throughout the request. It also integrates with FastAPI by adding middleware that
243
+ handles request and response logging.
201
244
  """
202
245
 
203
246
  REQUEST_LOGGER_SUFFIX: str = '-request-logger'
204
247
 
205
248
  def __init__(
206
249
  self,
207
- app: FastAPI,
250
+ app: Starlette,
208
251
  request_logger_name: Optional[str] = None,
209
252
  log_payload: bool = True,
210
253
  *args, **kwargs
@@ -213,12 +256,10 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
213
256
  Initialize the handler.
214
257
 
215
258
  Args:
216
- app (FastAPI): The FastAPI application instance.
259
+ app (FastAPI | Starlette): The FastAPI or Starlette application instance.
217
260
  request_logger_name (Optional[str]): The name of the Cloud Logging logger to use for request logs.
218
261
  Defaults to the Google Cloud Project ID with '-request-logger' suffix.
219
- log_payload (bool): Whether to log the request payload. If True, the payload for
220
- POST, PUT, PATCH, and DELETE requests will be logged.
221
- Defaults to True.
262
+ log_payload (bool): Whether to log the request payload for certain HTTP methods. Defaults to True.
222
263
  *args: Additional arguments to pass to the superclass constructor.
223
264
  Any argument you would pass to CloudLoggingHandler.
224
265
  **kwargs: Additional keyword arguments to pass to the superclass constructor.
@@ -1,21 +1,21 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fastapi-gae-logging
3
- Version: 0.0.6
4
- Summary: Custom Cloud Logging handler for FastAPI applications deployed in Google App Engine. Groups logs coming from the same request lifecycle and propagates the maximum log level throughout the request lifecycle using middleware and context management.
3
+ Version: 0.0.8
4
+ Summary: Custom Cloud Logging handler for FastAPI (or any Starlette based) applications deployed in Google App Engine. Groups logs coming from the same request lifecycle and propagates the maximum log level throughout the request lifecycle using middleware and context management.
5
5
  Home-page: https://github.com/chrisK824/fastapi-gae-logging
6
6
  Author: Chris Karvouniaris
7
7
  Author-email: christos.karvouniaris247@gmail.com
8
8
  License: MIT
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Operating System :: OS Independent
11
- Requires-Python: >=3.7
11
+ Requires-Python: >=3.8
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
- Requires-Dist: fastapi>=0.112.2
15
- Requires-Dist: google-cloud-logging>=3.11.2
14
+ Requires-Dist: starlette
15
+ Requires-Dist: google-cloud-logging
16
16
 
17
17
  # fastapi-gae-logging
18
- Custom Cloud Logging handler for FastAPI applications deployed in Google App Engine to ease out logs analysis and monitoring through Google Cloud Log Explorer.
18
+ Custom Cloud Logging handler for FastAPI (or any Starlette based) applications deployed in Google App Engine to ease out logs analysis and monitoring through Google Cloud Log Explorer.
19
19
 
20
20
  ## What problem does this package solve? Why do we need this?
21
21
  When deploying FastAPI applications on Google App Engine, I encountered several challenges with logging and observability even when using the official package for this purpose, `google-cloud-logging`.
@@ -57,7 +57,7 @@ The `fastapi-gae-logging` module addresses these problems by:
57
57
 
58
58
  ```python
59
59
  FastAPIGAELoggingHandler(
60
- app: FastAPI,
60
+ app: Starlette,
61
61
  request_logger_name: Optional[str] = None,
62
62
  log_payload: bool = True,
63
63
  *args, **kwargs
@@ -65,7 +65,7 @@ FastAPIGAELoggingHandler(
65
65
  ```
66
66
 
67
67
  - Parameters
68
- - **app** (FastAPI): The FastAPI application instance.
68
+ - **app** (FastAPI | Starlette): The FastAPI or Starlette application instance.
69
69
  - **request_logger_name** (Optional[str], optional): The name of the Cloud Logging logger to use for request logs.
70
70
  Defaults to the Google Cloud Project ID with the suffix '-request-logger'.
71
71
 
@@ -166,7 +166,7 @@ def post_payload(payload: Any = Body(None)):
166
166
  ## Dependencies
167
167
  This tool is built upon the following packages:
168
168
 
169
- - `fastapi`: A modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
169
+ - `starlette`: A modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
170
170
  - `google-cloud-logging`: Google Cloud Logging API client library for logging and managing logs in Google Cloud Platform.
171
171
 
172
172
 
@@ -0,0 +1,2 @@
1
+ starlette
2
+ google-cloud-logging
@@ -2,8 +2,8 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="fastapi-gae-logging",
5
- version="0.0.6",
6
- description="Custom Cloud Logging handler for FastAPI applications deployed in Google App Engine. \
5
+ version="0.0.8",
6
+ description="Custom Cloud Logging handler for FastAPI (or any Starlette based) applications deployed in Google App Engine. \
7
7
  Groups logs coming from the same request lifecycle and propagates the maximum log level \
8
8
  throughout the request lifecycle using middleware and context management.",
9
9
  long_description=open("README.md").read(),
@@ -13,10 +13,10 @@ setup(
13
13
  author_email="christos.karvouniaris247@gmail.com",
14
14
  packages=find_packages(),
15
15
  install_requires=[
16
- "fastapi>=0.112.2",
17
- "google-cloud-logging>=3.11.2",
16
+ "starlette",
17
+ "google-cloud-logging",
18
18
  ],
19
- python_requires=">=3.7",
19
+ python_requires=">=3.8",
20
20
  license="MIT",
21
21
  license_files=["LICENSE"],
22
22
  classifiers=[
@@ -1,2 +0,0 @@
1
- fastapi>=0.112.2
2
- google-cloud-logging>=3.11.2