fastapi-gae-logging 0.0.3__tar.gz → 0.0.5__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.3
3
+ Version: 0.0.5
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
@@ -74,6 +74,8 @@ if os.getenv('GAE_ENV', '').startswith('standard'):
74
74
 
75
75
  client = google.cloud.logging.Client()
76
76
  gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client)
77
+ # use the log_payload parameter if you want to opt-out from payload logging
78
+ # gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client, log_payload=False)
77
79
  setup_logging(handler=gae_log_handler)
78
80
 
79
81
  logging.getLogger().setLevel(logging.DEBUG)
@@ -132,14 +134,14 @@ def post_payload(payload: Any = Body(None)):
132
134
  ## How it looks in Google Cloud Log Explorer
133
135
 
134
136
  ### Logger selection
135
- ![alt text](<logger_selection.jpg>)
137
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/logger_selection.jpg)
136
138
 
137
139
  ### Groupped logs with propagated log severity to the parent log
138
140
 
139
- ![alt text](<groupped_logs.jpg>)
141
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/groupped_logs.jpg)
140
142
 
141
143
  ### Grouped logs in request with payload
142
- ![alt text](<request_with_payload.jpg>)
144
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/request_with_payload.jpg)
143
145
 
144
146
  ## Dependencies
145
147
  This tool is built upon the following packages:
@@ -155,8 +157,3 @@ This tool is built upon the following packages:
155
157
  - **Cloud Logging**: Utilizes Google Cloud Logging to group logs by request and propagate the maximum log level, enhancing observability and troubleshooting.
156
158
  - **Structured Logging**: Parent log of the request-response lifecycle is structured and sent to Google Cloud Logging with additional context, such as the request method, URL, and user agent after the request has been processed and served.
157
159
 
158
-
159
- ## Roadmap
160
- - Allow for opting out of the dictionary enforcement of logged payload.
161
- - Allow for opting out of request payload logging at all.
162
- - Explore other fields in Google Cloud Logs. Investigate and consider utilizing additional fields available in Google Cloud Logs that may allow for more goodies.
@@ -58,6 +58,8 @@ if os.getenv('GAE_ENV', '').startswith('standard'):
58
58
 
59
59
  client = google.cloud.logging.Client()
60
60
  gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client)
61
+ # use the log_payload parameter if you want to opt-out from payload logging
62
+ # gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client, log_payload=False)
61
63
  setup_logging(handler=gae_log_handler)
62
64
 
63
65
  logging.getLogger().setLevel(logging.DEBUG)
@@ -116,14 +118,14 @@ def post_payload(payload: Any = Body(None)):
116
118
  ## How it looks in Google Cloud Log Explorer
117
119
 
118
120
  ### Logger selection
119
- ![alt text](<logger_selection.jpg>)
121
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/logger_selection.jpg)
120
122
 
121
123
  ### Groupped logs with propagated log severity to the parent log
122
124
 
123
- ![alt text](<groupped_logs.jpg>)
125
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/groupped_logs.jpg)
124
126
 
125
127
  ### Grouped logs in request with payload
126
- ![alt text](<request_with_payload.jpg>)
128
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/request_with_payload.jpg)
127
129
 
128
130
  ## Dependencies
129
131
  This tool is built upon the following packages:
@@ -139,8 +141,3 @@ This tool is built upon the following packages:
139
141
  - **Cloud Logging**: Utilizes Google Cloud Logging to group logs by request and propagate the maximum log level, enhancing observability and troubleshooting.
140
142
  - **Structured Logging**: Parent log of the request-response lifecycle is structured and sent to Google Cloud Logging with additional context, such as the request method, URL, and user agent after the request has been processed and served.
141
143
 
142
-
143
- ## Roadmap
144
- - Allow for opting out of the dictionary enforcement of logged payload.
145
- - Allow for opting out of request payload logging at all.
146
- - Explore other fields in Google Cloud Logs. Investigate and consider utilizing additional fields available in Google Cloud Logs that may allow for more goodies.
@@ -65,7 +65,7 @@ class GAERequestLogger:
65
65
  logging.CRITICAL: 'CRITICAL',
66
66
  }
67
67
 
68
- def __init__(self, logger: Logger, resource: Resource) -> None:
68
+ def __init__(self, logger: Logger, resource: Resource, log_payload: bool = True) -> None:
69
69
  """
70
70
  Initialize the GAERequestLogger.
71
71
 
@@ -75,6 +75,7 @@ class GAERequestLogger:
75
75
  """
76
76
  self.logger = logger
77
77
  self.resource = resource
78
+ self.log_payload = log_payload
78
79
 
79
80
  def _log_level_to_severity(self, log_level: int) -> str:
80
81
  """
@@ -117,16 +118,19 @@ class GAERequestLogger:
117
118
  }
118
119
 
119
120
  payload = {}
120
- if request.method in ('POST', 'PUT', 'PATCH', 'DELETE'):
121
+
122
+ if self.log_payload and request.method in {'POST', 'PUT', 'PATCH', 'DELETE'}:
121
123
  try:
122
124
  payload = await request.json()
123
125
  except json.JSONDecodeError:
124
- pass
125
-
126
- if not isinstance(payload, dict):
127
- payload = {
128
- f"{type(payload).__name__}_payload_wrapper": payload
129
- }
126
+ logging.warning("Failed to decode request payload as JSON, skipping logging.")
127
+ except Exception as e:
128
+ logging.error(f"Unexpected error while logging payload: {e}")
129
+ else:
130
+ if not isinstance(payload, dict):
131
+ payload = {
132
+ f"{type(payload).__name__}_payload_wrapper": payload
133
+ }
130
134
 
131
135
  self.logger.log_struct(
132
136
  info=payload,
@@ -202,6 +206,7 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
202
206
  self,
203
207
  app: FastAPI,
204
208
  request_logger_name: Optional[str] = None,
209
+ log_payload: bool = True,
205
210
  *args, **kwargs
206
211
  ) -> None:
207
212
  """
@@ -211,6 +216,9 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
211
216
  app (FastAPI): The FastAPI application instance.
212
217
  request_logger_name (Optional[str]): The name of the Cloud Logging logger to use for request logs.
213
218
  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.
214
222
  *args: Additional arguments to pass to the superclass constructor.
215
223
  Any argument you would pass to CloudLoggingHandler.
216
224
  **kwargs: Additional keyword arguments to pass to the superclass constructor.
@@ -225,7 +233,8 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
225
233
  name=request_logger_name or f"{os.getenv('GOOGLE_CLOUD_PROJECT')}{self.REQUEST_LOGGER_SUFFIX}",
226
234
  resource=self.resource
227
235
  ),
228
- resource=self.resource
236
+ resource=self.resource,
237
+ log_payload=log_payload
229
238
  )
230
239
  )
231
240
  self.addFilter(LogInterceptor())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fastapi-gae-logging
3
- Version: 0.0.3
3
+ Version: 0.0.5
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
@@ -74,6 +74,8 @@ if os.getenv('GAE_ENV', '').startswith('standard'):
74
74
 
75
75
  client = google.cloud.logging.Client()
76
76
  gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client)
77
+ # use the log_payload parameter if you want to opt-out from payload logging
78
+ # gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client, log_payload=False)
77
79
  setup_logging(handler=gae_log_handler)
78
80
 
79
81
  logging.getLogger().setLevel(logging.DEBUG)
@@ -132,14 +134,14 @@ def post_payload(payload: Any = Body(None)):
132
134
  ## How it looks in Google Cloud Log Explorer
133
135
 
134
136
  ### Logger selection
135
- ![alt text](<logger_selection.jpg>)
137
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/logger_selection.jpg)
136
138
 
137
139
  ### Groupped logs with propagated log severity to the parent log
138
140
 
139
- ![alt text](<groupped_logs.jpg>)
141
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/groupped_logs.jpg)
140
142
 
141
143
  ### Grouped logs in request with payload
142
- ![alt text](<request_with_payload.jpg>)
144
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/request_with_payload.jpg)
143
145
 
144
146
  ## Dependencies
145
147
  This tool is built upon the following packages:
@@ -155,8 +157,3 @@ This tool is built upon the following packages:
155
157
  - **Cloud Logging**: Utilizes Google Cloud Logging to group logs by request and propagate the maximum log level, enhancing observability and troubleshooting.
156
158
  - **Structured Logging**: Parent log of the request-response lifecycle is structured and sent to Google Cloud Logging with additional context, such as the request method, URL, and user agent after the request has been processed and served.
157
159
 
158
-
159
- ## Roadmap
160
- - Allow for opting out of the dictionary enforcement of logged payload.
161
- - Allow for opting out of request payload logging at all.
162
- - Explore other fields in Google Cloud Logs. Investigate and consider utilizing additional fields available in Google Cloud Logs that may allow for more goodies.
@@ -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.3",
5
+ version="0.0.5",
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.",