fastapi-gae-logging 0.0.8__tar.gz → 0.0.9__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.
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/PKG-INFO +31 -8
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/README.md +30 -7
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging/fastapi_gae_logging.py +78 -16
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/PKG-INFO +31 -8
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/setup.py +1 -1
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/LICENSE +0 -0
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging/__init__.py +0 -0
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/SOURCES.txt +0 -0
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/dependency_links.txt +0 -0
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/requires.txt +0 -0
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/top_level.txt +0 -0
- {fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: fastapi-gae-logging
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.9
|
|
4
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
|
|
@@ -45,11 +45,12 @@ The `fastapi-gae-logging` module addresses these problems by:
|
|
|
45
45
|
|
|
46
46
|
- **Request Logs Grouping**: Groups logs from the same request lifecycle to simplify log analysis using Google Cloud Log Explorer. The logger name for grouping can be customized and defaults to the Google Cloud Project ID with '-request-logger' as a suffix.
|
|
47
47
|
- **Request Maximum Log Level Propagation**: Propagates the maximum log level throughout the request lifecycle, making it easier to search logs based on the severity of an issue.
|
|
48
|
-
- **
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
-
|
|
52
|
-
|
|
48
|
+
- **Optional incoming request logging**: Opt in/out to log headers and payload of incoming requests into the `jsonPayload` field of the parent log.
|
|
49
|
+
- **Optional request headers logging**: Defaults to True. Headers dict lands into field `request_headers` in the `jsonPayload` of parent log.
|
|
50
|
+
- **Request Payload Logging**: Defaults to True. Incoming payload parsed lands into field `request_payload` in the `jsonPayload` of parent log. Parsing is based on content type with capability to override. Currenty embedded parsers for:
|
|
51
|
+
- `application/json`
|
|
52
|
+
- `application/x-www-form-urlencoded`
|
|
53
|
+
- `text/plain`
|
|
53
54
|
|
|
54
55
|
## API
|
|
55
56
|
|
|
@@ -60,6 +61,8 @@ FastAPIGAELoggingHandler(
|
|
|
60
61
|
app: Starlette,
|
|
61
62
|
request_logger_name: Optional[str] = None,
|
|
62
63
|
log_payload: bool = True,
|
|
64
|
+
log_headers: bool = True,
|
|
65
|
+
custom_payload_parsers: Dict[str, Callable] = None,
|
|
63
66
|
*args, **kwargs
|
|
64
67
|
)
|
|
65
68
|
```
|
|
@@ -71,6 +74,9 @@ FastAPIGAELoggingHandler(
|
|
|
71
74
|
|
|
72
75
|
- **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
76
|
|
|
77
|
+
- **log_headers** (bool, optional): Whether to log the request headers. Defaults to True.
|
|
78
|
+
- **custom_payload_parsers** (Dict[str, Callable], optional): A dictionary mapping content types to custom parser functions for logging request payloads. If provided, these will override default parsers. Defaults to None.
|
|
79
|
+
|
|
74
80
|
- ***args**: Additional arguments to pass to the superclass constructor. Any argument you would pass to CloudLoggingHandler.
|
|
75
81
|
|
|
76
82
|
- ****kwargs**: Additional keyword arguments to pass to the superclass constructor. Any keyword argument you would pass to CloudLoggingHandler.
|
|
@@ -87,13 +93,30 @@ import os
|
|
|
87
93
|
|
|
88
94
|
app = FastAPI()
|
|
89
95
|
|
|
96
|
+
async def custom_payload_parser_plain_text(request: Request):
|
|
97
|
+
try:
|
|
98
|
+
body_bytes = await request.body()
|
|
99
|
+
incoming_payload = body_bytes.decode('utf-8')
|
|
100
|
+
return f"This was the original request payload: {incoming_payload}"
|
|
101
|
+
except Exception as e:
|
|
102
|
+
return f"Failed to read request payload as plain text: {e} | {traceback.format_exc()}"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
90
106
|
if os.getenv('GAE_ENV', '').startswith('standard'):
|
|
91
107
|
import google.cloud.logging
|
|
92
108
|
from google.cloud.logging_v2.handlers import setup_logging
|
|
93
109
|
from fastapi_gae_logging import FastAPIGAELoggingHandler
|
|
94
110
|
|
|
95
111
|
client = google.cloud.logging.Client()
|
|
96
|
-
|
|
112
|
+
# overriding default parsing for payload when content type is 'text/plain'
|
|
113
|
+
gae_log_handler = FastAPIGAELoggingHandler(
|
|
114
|
+
app=app,
|
|
115
|
+
client=client,
|
|
116
|
+
custom_payload_parsers={
|
|
117
|
+
"text/plain": custom_payload_parser_plain_text
|
|
118
|
+
}
|
|
119
|
+
)
|
|
97
120
|
# use the log_payload parameter if you want to opt-out from payload logging
|
|
98
121
|
# gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client, log_payload=False)
|
|
99
122
|
setup_logging(handler=gae_log_handler)
|
|
@@ -166,7 +189,7 @@ def post_payload(payload: Any = Body(None)):
|
|
|
166
189
|
## Dependencies
|
|
167
190
|
This tool is built upon the following packages:
|
|
168
191
|
|
|
169
|
-
- `starlette`:
|
|
192
|
+
- `starlette`: Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python. FastAPI is built on top of Starlette.
|
|
170
193
|
- `google-cloud-logging`: Google Cloud Logging API client library for logging and managing logs in Google Cloud Platform.
|
|
171
194
|
|
|
172
195
|
|
|
@@ -29,11 +29,12 @@ The `fastapi-gae-logging` module addresses these problems by:
|
|
|
29
29
|
|
|
30
30
|
- **Request Logs Grouping**: Groups logs from the same request lifecycle to simplify log analysis using Google Cloud Log Explorer. The logger name for grouping can be customized and defaults to the Google Cloud Project ID with '-request-logger' as a suffix.
|
|
31
31
|
- **Request Maximum Log Level Propagation**: Propagates the maximum log level throughout the request lifecycle, making it easier to search logs based on the severity of an issue.
|
|
32
|
-
- **
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
-
|
|
36
|
-
|
|
32
|
+
- **Optional incoming request logging**: Opt in/out to log headers and payload of incoming requests into the `jsonPayload` field of the parent log.
|
|
33
|
+
- **Optional request headers logging**: Defaults to True. Headers dict lands into field `request_headers` in the `jsonPayload` of parent log.
|
|
34
|
+
- **Request Payload Logging**: Defaults to True. Incoming payload parsed lands into field `request_payload` in the `jsonPayload` of parent log. Parsing is based on content type with capability to override. Currenty embedded parsers for:
|
|
35
|
+
- `application/json`
|
|
36
|
+
- `application/x-www-form-urlencoded`
|
|
37
|
+
- `text/plain`
|
|
37
38
|
|
|
38
39
|
## API
|
|
39
40
|
|
|
@@ -44,6 +45,8 @@ FastAPIGAELoggingHandler(
|
|
|
44
45
|
app: Starlette,
|
|
45
46
|
request_logger_name: Optional[str] = None,
|
|
46
47
|
log_payload: bool = True,
|
|
48
|
+
log_headers: bool = True,
|
|
49
|
+
custom_payload_parsers: Dict[str, Callable] = None,
|
|
47
50
|
*args, **kwargs
|
|
48
51
|
)
|
|
49
52
|
```
|
|
@@ -55,6 +58,9 @@ FastAPIGAELoggingHandler(
|
|
|
55
58
|
|
|
56
59
|
- **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
60
|
|
|
61
|
+
- **log_headers** (bool, optional): Whether to log the request headers. Defaults to True.
|
|
62
|
+
- **custom_payload_parsers** (Dict[str, Callable], optional): A dictionary mapping content types to custom parser functions for logging request payloads. If provided, these will override default parsers. Defaults to None.
|
|
63
|
+
|
|
58
64
|
- ***args**: Additional arguments to pass to the superclass constructor. Any argument you would pass to CloudLoggingHandler.
|
|
59
65
|
|
|
60
66
|
- ****kwargs**: Additional keyword arguments to pass to the superclass constructor. Any keyword argument you would pass to CloudLoggingHandler.
|
|
@@ -71,13 +77,30 @@ import os
|
|
|
71
77
|
|
|
72
78
|
app = FastAPI()
|
|
73
79
|
|
|
80
|
+
async def custom_payload_parser_plain_text(request: Request):
|
|
81
|
+
try:
|
|
82
|
+
body_bytes = await request.body()
|
|
83
|
+
incoming_payload = body_bytes.decode('utf-8')
|
|
84
|
+
return f"This was the original request payload: {incoming_payload}"
|
|
85
|
+
except Exception as e:
|
|
86
|
+
return f"Failed to read request payload as plain text: {e} | {traceback.format_exc()}"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
74
90
|
if os.getenv('GAE_ENV', '').startswith('standard'):
|
|
75
91
|
import google.cloud.logging
|
|
76
92
|
from google.cloud.logging_v2.handlers import setup_logging
|
|
77
93
|
from fastapi_gae_logging import FastAPIGAELoggingHandler
|
|
78
94
|
|
|
79
95
|
client = google.cloud.logging.Client()
|
|
80
|
-
|
|
96
|
+
# overriding default parsing for payload when content type is 'text/plain'
|
|
97
|
+
gae_log_handler = FastAPIGAELoggingHandler(
|
|
98
|
+
app=app,
|
|
99
|
+
client=client,
|
|
100
|
+
custom_payload_parsers={
|
|
101
|
+
"text/plain": custom_payload_parser_plain_text
|
|
102
|
+
}
|
|
103
|
+
)
|
|
81
104
|
# use the log_payload parameter if you want to opt-out from payload logging
|
|
82
105
|
# gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client, log_payload=False)
|
|
83
106
|
setup_logging(handler=gae_log_handler)
|
|
@@ -150,7 +173,7 @@ def post_payload(payload: Any = Body(None)):
|
|
|
150
173
|
## Dependencies
|
|
151
174
|
This tool is built upon the following packages:
|
|
152
175
|
|
|
153
|
-
- `starlette`:
|
|
176
|
+
- `starlette`: Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python. FastAPI is built on top of Starlette.
|
|
154
177
|
- `google-cloud-logging`: Google Cloud Logging API client library for logging and managing logs in Google Cloud Platform.
|
|
155
178
|
|
|
156
179
|
|
{fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging/fastapi_gae_logging.py
RENAMED
|
@@ -3,8 +3,7 @@ import time
|
|
|
3
3
|
import contextvars
|
|
4
4
|
import os
|
|
5
5
|
import re
|
|
6
|
-
import
|
|
7
|
-
from typing import Optional, Dict, Any
|
|
6
|
+
from typing import Optional, Dict, Any, Callable
|
|
8
7
|
from starlette.applications import Starlette
|
|
9
8
|
from starlette.exceptions import HTTPException
|
|
10
9
|
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
@@ -12,6 +11,7 @@ from starlette.responses import Response
|
|
|
12
11
|
from starlette.requests import Request
|
|
13
12
|
from google.cloud.logging_v2.handlers import CloudLoggingHandler
|
|
14
13
|
from google.cloud.logging_v2 import Resource, Logger
|
|
14
|
+
import traceback
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
gae_request_context = contextvars.ContextVar('gae_request_context', default={
|
|
@@ -55,6 +55,44 @@ class LogInterceptor(logging.Filter):
|
|
|
55
55
|
return True
|
|
56
56
|
|
|
57
57
|
|
|
58
|
+
class RequestPayloadParser:
|
|
59
|
+
def __init__(self, custom_parsers: Dict[str, Callable] = None):
|
|
60
|
+
self.parsers = {
|
|
61
|
+
"application/json": self._parse_json,
|
|
62
|
+
"application/x-www-form-urlencoded": self._parse_form_urlencoded,
|
|
63
|
+
"text/plain": self._parse_plain_text,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if custom_parsers:
|
|
67
|
+
self.parsers.update(custom_parsers)
|
|
68
|
+
|
|
69
|
+
async def _parse_json(self, request: Request):
|
|
70
|
+
try:
|
|
71
|
+
return await request.json()
|
|
72
|
+
except Exception as e:
|
|
73
|
+
return f"Failed to decode request payload as JSON: {e} | {traceback.format_exc()}"
|
|
74
|
+
|
|
75
|
+
async def _parse_form_urlencoded(self, request: Request):
|
|
76
|
+
try:
|
|
77
|
+
form = await request.form()
|
|
78
|
+
return dict(form)
|
|
79
|
+
except Exception as e:
|
|
80
|
+
return f"Failed to parse request form data: {e} | {traceback.format_exc()}"
|
|
81
|
+
|
|
82
|
+
async def _parse_plain_text(self, request: Request):
|
|
83
|
+
try:
|
|
84
|
+
body_bytes = await request.body()
|
|
85
|
+
return body_bytes.decode('utf-8')
|
|
86
|
+
except Exception as e:
|
|
87
|
+
return f"Failed to read request payload as plain text: {e} | {traceback.format_exc()}"
|
|
88
|
+
|
|
89
|
+
def get_parser(self, content_type: str):
|
|
90
|
+
"""
|
|
91
|
+
Returns the parser function for the given content type.
|
|
92
|
+
"""
|
|
93
|
+
return self.parsers.get(content_type)
|
|
94
|
+
|
|
95
|
+
|
|
58
96
|
class GAERequestLogger:
|
|
59
97
|
"""
|
|
60
98
|
A logger for emitting structured request logs in Google App Engine.
|
|
@@ -69,6 +107,10 @@ class GAERequestLogger:
|
|
|
69
107
|
logger (Logger): The Google Cloud Logger instance to log requests.
|
|
70
108
|
resource (Resource): The Google Cloud resource associated with the logger.
|
|
71
109
|
log_payload (bool): Whether to log the request payload for certain HTTP methods. Defaults to True.
|
|
110
|
+
log_headers (bool): Whether to log the request headers. Defaults to True.
|
|
111
|
+
custom_payload_parsers (Dict[str, Callable], optional): A dictionary mapping content types to custom
|
|
112
|
+
parser functions for logging request payloads. If provided, these will override default parsers.
|
|
113
|
+
Defaults to None.
|
|
72
114
|
"""
|
|
73
115
|
LOG_LEVEL_TO_SEVERITY: Dict[int, str] = {
|
|
74
116
|
logging.NOTSET: 'DEFAULT',
|
|
@@ -79,7 +121,8 @@ class GAERequestLogger:
|
|
|
79
121
|
logging.CRITICAL: 'CRITICAL',
|
|
80
122
|
}
|
|
81
123
|
|
|
82
|
-
def __init__(self, logger: Logger, resource: Resource, log_payload: bool = True
|
|
124
|
+
def __init__(self, logger: Logger, resource: Resource, log_payload: bool = True, log_headers: bool = True,
|
|
125
|
+
custom_payload_parsers: Dict[str, Callable] = None) -> None:
|
|
83
126
|
"""
|
|
84
127
|
Initialize the GAERequestLogger.
|
|
85
128
|
|
|
@@ -87,10 +130,16 @@ class GAERequestLogger:
|
|
|
87
130
|
logger (Logger): The Google Cloud Logger instance to log requests.
|
|
88
131
|
resource (Resource): The resource associated with the logger.
|
|
89
132
|
log_payload (bool): Whether to log the request payload for certain HTTP methods. Defaults to True.
|
|
133
|
+
log_headers (bool): Whether to log the request headers. Defaults to True.
|
|
134
|
+
custom_payload_parsers (Dict[str, Callable], optional): A dictionary mapping content types to custom
|
|
135
|
+
parser functions for logging request payloads. If provided, these will override default parsers.
|
|
136
|
+
Defaults to None.
|
|
90
137
|
"""
|
|
91
138
|
self.logger = logger
|
|
92
139
|
self.resource = resource
|
|
93
140
|
self.log_payload = log_payload
|
|
141
|
+
self.log_headers = log_headers
|
|
142
|
+
self.payload_parsers = RequestPayloadParser(custom_payload_parsers)
|
|
94
143
|
|
|
95
144
|
def _log_level_to_severity(self, log_level: int) -> str:
|
|
96
145
|
"""
|
|
@@ -132,23 +181,28 @@ class GAERequestLogger:
|
|
|
132
181
|
'remoteIp': request.client.host
|
|
133
182
|
}
|
|
134
183
|
|
|
135
|
-
|
|
184
|
+
logging_payload = {}
|
|
185
|
+
|
|
186
|
+
if self.log_headers:
|
|
187
|
+
logging_payload['request_headers'] = dict(request.headers)
|
|
136
188
|
|
|
137
189
|
if self.log_payload and request.method in {'POST', 'PUT', 'PATCH', 'DELETE'}:
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
logging.error(f"Unexpected error while logging payload: {e}")
|
|
190
|
+
content_type = request.headers.get("content-type", "").split(";")[0].strip()
|
|
191
|
+
payload_parser = self.payload_parsers.get_parser(content_type)
|
|
192
|
+
|
|
193
|
+
if not payload_parser:
|
|
194
|
+
request_payload = f"Unsupported content type {content_type}. Skipping payload logging."
|
|
144
195
|
else:
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
196
|
+
try:
|
|
197
|
+
request_payload = await payload_parser(request)
|
|
198
|
+
except Exception as e:
|
|
199
|
+
request_payload = f"Parser of request payload for content type {content_type} failed: {e} | {traceback.format_exc()}"
|
|
200
|
+
|
|
201
|
+
if request_payload:
|
|
202
|
+
logging_payload['request_payload'] = request_payload
|
|
149
203
|
|
|
150
204
|
self.logger.log_struct(
|
|
151
|
-
info=
|
|
205
|
+
info=logging_payload,
|
|
152
206
|
resource=self.resource,
|
|
153
207
|
trace=f"projects/{os.environ['GOOGLE_CLOUD_PROJECT']}/traces/{trace.split('/', 1)[0]}",
|
|
154
208
|
http_request=http_request,
|
|
@@ -250,6 +304,8 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
|
|
|
250
304
|
app: Starlette,
|
|
251
305
|
request_logger_name: Optional[str] = None,
|
|
252
306
|
log_payload: bool = True,
|
|
307
|
+
log_headers: bool = True,
|
|
308
|
+
custom_payload_parsers: Dict[str, Callable] = None,
|
|
253
309
|
*args, **kwargs
|
|
254
310
|
) -> None:
|
|
255
311
|
"""
|
|
@@ -260,6 +316,10 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
|
|
|
260
316
|
request_logger_name (Optional[str]): The name of the Cloud Logging logger to use for request logs.
|
|
261
317
|
Defaults to the Google Cloud Project ID with '-request-logger' suffix.
|
|
262
318
|
log_payload (bool): Whether to log the request payload for certain HTTP methods. Defaults to True.
|
|
319
|
+
log_headers (bool): Whether to log the request headers. Defaults to True.
|
|
320
|
+
custom_payload_parsers (Dict[str, Callable], optional): A dictionary mapping content types to custom
|
|
321
|
+
parser functions for logging request payloads. If provided, these will override default parsers.
|
|
322
|
+
Defaults to None.
|
|
263
323
|
*args: Additional arguments to pass to the superclass constructor.
|
|
264
324
|
Any argument you would pass to CloudLoggingHandler.
|
|
265
325
|
**kwargs: Additional keyword arguments to pass to the superclass constructor.
|
|
@@ -275,7 +335,9 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
|
|
|
275
335
|
resource=self.resource
|
|
276
336
|
),
|
|
277
337
|
resource=self.resource,
|
|
278
|
-
log_payload=log_payload
|
|
338
|
+
log_payload=log_payload,
|
|
339
|
+
log_headers=log_headers,
|
|
340
|
+
custom_payload_parsers=custom_payload_parsers
|
|
279
341
|
)
|
|
280
342
|
)
|
|
281
343
|
self.addFilter(LogInterceptor())
|
{fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: fastapi-gae-logging
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.9
|
|
4
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
|
|
@@ -45,11 +45,12 @@ The `fastapi-gae-logging` module addresses these problems by:
|
|
|
45
45
|
|
|
46
46
|
- **Request Logs Grouping**: Groups logs from the same request lifecycle to simplify log analysis using Google Cloud Log Explorer. The logger name for grouping can be customized and defaults to the Google Cloud Project ID with '-request-logger' as a suffix.
|
|
47
47
|
- **Request Maximum Log Level Propagation**: Propagates the maximum log level throughout the request lifecycle, making it easier to search logs based on the severity of an issue.
|
|
48
|
-
- **
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
-
|
|
52
|
-
|
|
48
|
+
- **Optional incoming request logging**: Opt in/out to log headers and payload of incoming requests into the `jsonPayload` field of the parent log.
|
|
49
|
+
- **Optional request headers logging**: Defaults to True. Headers dict lands into field `request_headers` in the `jsonPayload` of parent log.
|
|
50
|
+
- **Request Payload Logging**: Defaults to True. Incoming payload parsed lands into field `request_payload` in the `jsonPayload` of parent log. Parsing is based on content type with capability to override. Currenty embedded parsers for:
|
|
51
|
+
- `application/json`
|
|
52
|
+
- `application/x-www-form-urlencoded`
|
|
53
|
+
- `text/plain`
|
|
53
54
|
|
|
54
55
|
## API
|
|
55
56
|
|
|
@@ -60,6 +61,8 @@ FastAPIGAELoggingHandler(
|
|
|
60
61
|
app: Starlette,
|
|
61
62
|
request_logger_name: Optional[str] = None,
|
|
62
63
|
log_payload: bool = True,
|
|
64
|
+
log_headers: bool = True,
|
|
65
|
+
custom_payload_parsers: Dict[str, Callable] = None,
|
|
63
66
|
*args, **kwargs
|
|
64
67
|
)
|
|
65
68
|
```
|
|
@@ -71,6 +74,9 @@ FastAPIGAELoggingHandler(
|
|
|
71
74
|
|
|
72
75
|
- **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
76
|
|
|
77
|
+
- **log_headers** (bool, optional): Whether to log the request headers. Defaults to True.
|
|
78
|
+
- **custom_payload_parsers** (Dict[str, Callable], optional): A dictionary mapping content types to custom parser functions for logging request payloads. If provided, these will override default parsers. Defaults to None.
|
|
79
|
+
|
|
74
80
|
- ***args**: Additional arguments to pass to the superclass constructor. Any argument you would pass to CloudLoggingHandler.
|
|
75
81
|
|
|
76
82
|
- ****kwargs**: Additional keyword arguments to pass to the superclass constructor. Any keyword argument you would pass to CloudLoggingHandler.
|
|
@@ -87,13 +93,30 @@ import os
|
|
|
87
93
|
|
|
88
94
|
app = FastAPI()
|
|
89
95
|
|
|
96
|
+
async def custom_payload_parser_plain_text(request: Request):
|
|
97
|
+
try:
|
|
98
|
+
body_bytes = await request.body()
|
|
99
|
+
incoming_payload = body_bytes.decode('utf-8')
|
|
100
|
+
return f"This was the original request payload: {incoming_payload}"
|
|
101
|
+
except Exception as e:
|
|
102
|
+
return f"Failed to read request payload as plain text: {e} | {traceback.format_exc()}"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
90
106
|
if os.getenv('GAE_ENV', '').startswith('standard'):
|
|
91
107
|
import google.cloud.logging
|
|
92
108
|
from google.cloud.logging_v2.handlers import setup_logging
|
|
93
109
|
from fastapi_gae_logging import FastAPIGAELoggingHandler
|
|
94
110
|
|
|
95
111
|
client = google.cloud.logging.Client()
|
|
96
|
-
|
|
112
|
+
# overriding default parsing for payload when content type is 'text/plain'
|
|
113
|
+
gae_log_handler = FastAPIGAELoggingHandler(
|
|
114
|
+
app=app,
|
|
115
|
+
client=client,
|
|
116
|
+
custom_payload_parsers={
|
|
117
|
+
"text/plain": custom_payload_parser_plain_text
|
|
118
|
+
}
|
|
119
|
+
)
|
|
97
120
|
# use the log_payload parameter if you want to opt-out from payload logging
|
|
98
121
|
# gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client, log_payload=False)
|
|
99
122
|
setup_logging(handler=gae_log_handler)
|
|
@@ -166,7 +189,7 @@ def post_payload(payload: Any = Body(None)):
|
|
|
166
189
|
## Dependencies
|
|
167
190
|
This tool is built upon the following packages:
|
|
168
191
|
|
|
169
|
-
- `starlette`:
|
|
192
|
+
- `starlette`: Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python. FastAPI is built on top of Starlette.
|
|
170
193
|
- `google-cloud-logging`: Google Cloud Logging API client library for logging and managing logs in Google Cloud Platform.
|
|
171
194
|
|
|
172
195
|
|
|
@@ -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
|
+
version="0.0.9",
|
|
6
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.",
|
|
File without changes
|
|
File without changes
|
{fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/requires.txt
RENAMED
|
File without changes
|
{fastapi_gae_logging-0.0.8 → fastapi_gae_logging-0.0.9}/fastapi_gae_logging.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|