fastapi-gae-logging 0.0.5__tar.gz → 0.0.7__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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fastapi-gae-logging
3
- Version: 0.0.5
3
+ Version: 0.0.7
4
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.
5
5
  Home-page: https://github.com/chrisK824/fastapi-gae-logging
6
6
  Author: Chris Karvouniaris
@@ -8,11 +8,11 @@ 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: fastapi
15
+ Requires-Dist: google-cloud-logging
16
16
 
17
17
  # fastapi-gae-logging
18
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.
@@ -53,7 +53,27 @@ The `fastapi-gae-logging` module addresses these problems by:
53
53
 
54
54
  ## API
55
55
 
56
- - `FastAPIGAELoggingHandler` class: A custom Cloud Logging Handler for FastAPI applications to use with the official google-cloud-logging library.
56
+ - Initialization
57
+
58
+ ```python
59
+ FastAPIGAELoggingHandler(
60
+ app: FastAPI,
61
+ request_logger_name: Optional[str] = None,
62
+ log_payload: bool = True,
63
+ *args, **kwargs
64
+ )
65
+ ```
66
+
67
+ - Parameters
68
+ - **app** (FastAPI): The FastAPI application instance.
69
+ - **request_logger_name** (Optional[str], optional): The name of the Cloud Logging logger to use for request logs.
70
+ Defaults to the Google Cloud Project ID with the suffix '-request-logger'.
71
+
72
+ - **log_payload** (bool, optional): Whether to log the request payload. If True, the payload for POST, PUT, PATCH, and DELETE requests will be logged. Defaults to True.
73
+
74
+ - ***args**: Additional arguments to pass to the superclass constructor. Any argument you would pass to CloudLoggingHandler.
75
+
76
+ - ****kwargs**: Additional keyword arguments to pass to the superclass constructor. Any keyword argument you would pass to CloudLoggingHandler.
57
77
 
58
78
 
59
79
  ## Example of usage
@@ -1,19 +1,3 @@
1
- Metadata-Version: 2.1
2
- Name: fastapi-gae-logging
3
- Version: 0.0.5
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.
5
- Home-page: https://github.com/chrisK824/fastapi-gae-logging
6
- Author: Chris Karvouniaris
7
- Author-email: christos.karvouniaris247@gmail.com
8
- License: MIT
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: Operating System :: OS Independent
11
- Requires-Python: >=3.7
12
- Description-Content-Type: text/markdown
13
- License-File: LICENSE
14
- Requires-Dist: fastapi>=0.112.2
15
- Requires-Dist: google-cloud-logging>=3.11.2
16
-
17
1
  # fastapi-gae-logging
18
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.
19
3
 
@@ -53,7 +37,27 @@ The `fastapi-gae-logging` module addresses these problems by:
53
37
 
54
38
  ## API
55
39
 
56
- - `FastAPIGAELoggingHandler` class: A custom Cloud Logging Handler for FastAPI applications to use with the official google-cloud-logging library.
40
+ - Initialization
41
+
42
+ ```python
43
+ FastAPIGAELoggingHandler(
44
+ app: FastAPI,
45
+ request_logger_name: Optional[str] = None,
46
+ log_payload: bool = True,
47
+ *args, **kwargs
48
+ )
49
+ ```
50
+
51
+ - Parameters
52
+ - **app** (FastAPI): The FastAPI application instance.
53
+ - **request_logger_name** (Optional[str], optional): The name of the Cloud Logging logger to use for request logs.
54
+ Defaults to the Google Cloud Project ID with the suffix '-request-logger'.
55
+
56
+ - **log_payload** (bool, optional): Whether to log the request payload. If True, the payload for POST, PUT, PATCH, and DELETE requests will be logged. Defaults to True.
57
+
58
+ - ***args**: Additional arguments to pass to the superclass constructor. Any argument you would pass to CloudLoggingHandler.
59
+
60
+ - ****kwargs**: Additional keyword arguments to pass to the superclass constructor. Any keyword argument you would pass to CloudLoggingHandler.
57
61
 
58
62
 
59
63
  ## Example of usage
@@ -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,9 +236,11 @@ 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'
@@ -216,9 +259,7 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
216
259
  app (FastAPI): The FastAPI 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,3 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: fastapi-gae-logging
3
+ Version: 0.0.7
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.
5
+ Home-page: https://github.com/chrisK824/fastapi-gae-logging
6
+ Author: Chris Karvouniaris
7
+ Author-email: christos.karvouniaris247@gmail.com
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: fastapi
15
+ Requires-Dist: google-cloud-logging
16
+
1
17
  # fastapi-gae-logging
2
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.
3
19
 
@@ -37,7 +53,27 @@ The `fastapi-gae-logging` module addresses these problems by:
37
53
 
38
54
  ## API
39
55
 
40
- - `FastAPIGAELoggingHandler` class: A custom Cloud Logging Handler for FastAPI applications to use with the official google-cloud-logging library.
56
+ - Initialization
57
+
58
+ ```python
59
+ FastAPIGAELoggingHandler(
60
+ app: FastAPI,
61
+ request_logger_name: Optional[str] = None,
62
+ log_payload: bool = True,
63
+ *args, **kwargs
64
+ )
65
+ ```
66
+
67
+ - Parameters
68
+ - **app** (FastAPI): The FastAPI application instance.
69
+ - **request_logger_name** (Optional[str], optional): The name of the Cloud Logging logger to use for request logs.
70
+ Defaults to the Google Cloud Project ID with the suffix '-request-logger'.
71
+
72
+ - **log_payload** (bool, optional): Whether to log the request payload. If True, the payload for POST, PUT, PATCH, and DELETE requests will be logged. Defaults to True.
73
+
74
+ - ***args**: Additional arguments to pass to the superclass constructor. Any argument you would pass to CloudLoggingHandler.
75
+
76
+ - ****kwargs**: Additional keyword arguments to pass to the superclass constructor. Any keyword argument you would pass to CloudLoggingHandler.
41
77
 
42
78
 
43
79
  ## Example of usage
@@ -0,0 +1,2 @@
1
+ fastapi
2
+ google-cloud-logging
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="fastapi-gae-logging",
5
- version="0.0.5",
5
+ version="0.0.7",
6
6
  description="Custom Cloud Logging handler for FastAPI 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.",
@@ -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
+ "fastapi",
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