fastapi-gae-logging 0.0.3__tar.gz → 0.0.4__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.4
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
@@ -132,14 +132,14 @@ def post_payload(payload: Any = Body(None)):
132
132
  ## How it looks in Google Cloud Log Explorer
133
133
 
134
134
  ### Logger selection
135
- ![alt text](<logger_selection.jpg>)
135
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/logger_selection.jpg)
136
136
 
137
137
  ### Groupped logs with propagated log severity to the parent log
138
138
 
139
- ![alt text](<groupped_logs.jpg>)
139
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/groupped_logs.jpg)
140
140
 
141
141
  ### Grouped logs in request with payload
142
- ![alt text](<request_with_payload.jpg>)
142
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/request_with_payload.jpg)
143
143
 
144
144
  ## Dependencies
145
145
  This tool is built upon the following packages:
@@ -155,8 +155,3 @@ This tool is built upon the following packages:
155
155
  - **Cloud Logging**: Utilizes Google Cloud Logging to group logs by request and propagate the maximum log level, enhancing observability and troubleshooting.
156
156
  - **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
157
 
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.
@@ -116,14 +116,14 @@ def post_payload(payload: Any = Body(None)):
116
116
  ## How it looks in Google Cloud Log Explorer
117
117
 
118
118
  ### Logger selection
119
- ![alt text](<logger_selection.jpg>)
119
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/logger_selection.jpg)
120
120
 
121
121
  ### Groupped logs with propagated log severity to the parent log
122
122
 
123
- ![alt text](<groupped_logs.jpg>)
123
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/groupped_logs.jpg)
124
124
 
125
125
  ### Grouped logs in request with payload
126
- ![alt text](<request_with_payload.jpg>)
126
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/request_with_payload.jpg)
127
127
 
128
128
  ## Dependencies
129
129
  This tool is built upon the following packages:
@@ -139,8 +139,3 @@ This tool is built upon the following packages:
139
139
  - **Cloud Logging**: Utilizes Google Cloud Logging to group logs by request and propagate the maximum log level, enhancing observability and troubleshooting.
140
140
  - **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
141
 
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
  """
@@ -225,7 +230,8 @@ class FastAPIGAELoggingHandler(CloudLoggingHandler):
225
230
  name=request_logger_name or f"{os.getenv('GOOGLE_CLOUD_PROJECT')}{self.REQUEST_LOGGER_SUFFIX}",
226
231
  resource=self.resource
227
232
  ),
228
- resource=self.resource
233
+ resource=self.resource,
234
+ log_payload=log_payload
229
235
  )
230
236
  )
231
237
  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.4
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
@@ -132,14 +132,14 @@ def post_payload(payload: Any = Body(None)):
132
132
  ## How it looks in Google Cloud Log Explorer
133
133
 
134
134
  ### Logger selection
135
- ![alt text](<logger_selection.jpg>)
135
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/logger_selection.jpg)
136
136
 
137
137
  ### Groupped logs with propagated log severity to the parent log
138
138
 
139
- ![alt text](<groupped_logs.jpg>)
139
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/groupped_logs.jpg)
140
140
 
141
141
  ### Grouped logs in request with payload
142
- ![alt text](<request_with_payload.jpg>)
142
+ ![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/request_with_payload.jpg)
143
143
 
144
144
  ## Dependencies
145
145
  This tool is built upon the following packages:
@@ -155,8 +155,3 @@ This tool is built upon the following packages:
155
155
  - **Cloud Logging**: Utilizes Google Cloud Logging to group logs by request and propagate the maximum log level, enhancing observability and troubleshooting.
156
156
  - **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
157
 
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.4",
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.",