google-cloud-logging 3.13.0__py3-none-any.whl

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.
Files changed (68) hide show
  1. google/cloud/logging/__init__.py +53 -0
  2. google/cloud/logging/gapic_version.py +16 -0
  3. google/cloud/logging/handlers/__init__.py +31 -0
  4. google/cloud/logging/handlers/middleware/__init__.py +17 -0
  5. google/cloud/logging/handlers/transports/__init__.py +28 -0
  6. google/cloud/logging/py.typed +2 -0
  7. google/cloud/logging_v2/__init__.py +61 -0
  8. google/cloud/logging_v2/_gapic.py +647 -0
  9. google/cloud/logging_v2/_helpers.py +152 -0
  10. google/cloud/logging_v2/_http.py +549 -0
  11. google/cloud/logging_v2/_instrumentation.py +92 -0
  12. google/cloud/logging_v2/client.py +429 -0
  13. google/cloud/logging_v2/entries.py +358 -0
  14. google/cloud/logging_v2/gapic_metadata.json +481 -0
  15. google/cloud/logging_v2/gapic_version.py +16 -0
  16. google/cloud/logging_v2/handlers/__init__.py +31 -0
  17. google/cloud/logging_v2/handlers/_helpers.py +283 -0
  18. google/cloud/logging_v2/handlers/_monitored_resources.py +303 -0
  19. google/cloud/logging_v2/handlers/app_engine.py +139 -0
  20. google/cloud/logging_v2/handlers/container_engine.py +62 -0
  21. google/cloud/logging_v2/handlers/handlers.py +340 -0
  22. google/cloud/logging_v2/handlers/middleware/__init__.py +17 -0
  23. google/cloud/logging_v2/handlers/middleware/request.py +52 -0
  24. google/cloud/logging_v2/handlers/structured_log.py +150 -0
  25. google/cloud/logging_v2/handlers/transports/__init__.py +29 -0
  26. google/cloud/logging_v2/handlers/transports/background_thread.py +343 -0
  27. google/cloud/logging_v2/handlers/transports/base.py +61 -0
  28. google/cloud/logging_v2/handlers/transports/sync.py +68 -0
  29. google/cloud/logging_v2/logger.py +541 -0
  30. google/cloud/logging_v2/metric.py +180 -0
  31. google/cloud/logging_v2/py.typed +2 -0
  32. google/cloud/logging_v2/resource.py +51 -0
  33. google/cloud/logging_v2/services/__init__.py +15 -0
  34. google/cloud/logging_v2/services/config_service_v2/__init__.py +22 -0
  35. google/cloud/logging_v2/services/config_service_v2/async_client.py +4232 -0
  36. google/cloud/logging_v2/services/config_service_v2/client.py +4686 -0
  37. google/cloud/logging_v2/services/config_service_v2/pagers.py +820 -0
  38. google/cloud/logging_v2/services/config_service_v2/transports/__init__.py +33 -0
  39. google/cloud/logging_v2/services/config_service_v2/transports/base.py +748 -0
  40. google/cloud/logging_v2/services/config_service_v2/transports/grpc.py +1345 -0
  41. google/cloud/logging_v2/services/config_service_v2/transports/grpc_asyncio.py +1660 -0
  42. google/cloud/logging_v2/services/logging_service_v2/__init__.py +22 -0
  43. google/cloud/logging_v2/services/logging_service_v2/async_client.py +1272 -0
  44. google/cloud/logging_v2/services/logging_service_v2/client.py +1686 -0
  45. google/cloud/logging_v2/services/logging_service_v2/pagers.py +516 -0
  46. google/cloud/logging_v2/services/logging_service_v2/transports/__init__.py +33 -0
  47. google/cloud/logging_v2/services/logging_service_v2/transports/base.py +361 -0
  48. google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py +566 -0
  49. google/cloud/logging_v2/services/logging_service_v2/transports/grpc_asyncio.py +703 -0
  50. google/cloud/logging_v2/services/metrics_service_v2/__init__.py +22 -0
  51. google/cloud/logging_v2/services/metrics_service_v2/async_client.py +1119 -0
  52. google/cloud/logging_v2/services/metrics_service_v2/client.py +1534 -0
  53. google/cloud/logging_v2/services/metrics_service_v2/pagers.py +196 -0
  54. google/cloud/logging_v2/services/metrics_service_v2/transports/__init__.py +33 -0
  55. google/cloud/logging_v2/services/metrics_service_v2/transports/base.py +320 -0
  56. google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py +520 -0
  57. google/cloud/logging_v2/services/metrics_service_v2/transports/grpc_asyncio.py +629 -0
  58. google/cloud/logging_v2/sink.py +233 -0
  59. google/cloud/logging_v2/types/__init__.py +178 -0
  60. google/cloud/logging_v2/types/log_entry.py +435 -0
  61. google/cloud/logging_v2/types/logging.py +602 -0
  62. google/cloud/logging_v2/types/logging_config.py +2418 -0
  63. google/cloud/logging_v2/types/logging_metrics.py +412 -0
  64. google_cloud_logging-3.13.0.dist-info/METADATA +143 -0
  65. google_cloud_logging-3.13.0.dist-info/RECORD +68 -0
  66. google_cloud_logging-3.13.0.dist-info/WHEEL +5 -0
  67. google_cloud_logging-3.13.0.dist-info/licenses/LICENSE +202 -0
  68. google_cloud_logging-3.13.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,139 @@
1
+ # Copyright 2016 Google LLC All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Logging handler for App Engine Flexible
16
+
17
+ Sends logs to the Cloud Logging API with the appropriate resource
18
+ and labels for App Engine logs.
19
+ """
20
+
21
+ import logging
22
+ import os
23
+ import warnings
24
+
25
+ from google.cloud.logging_v2.handlers._helpers import get_request_data
26
+ from google.cloud.logging_v2.handlers._monitored_resources import (
27
+ _create_app_engine_resource,
28
+ )
29
+ from google.cloud.logging_v2.handlers.transports import BackgroundThreadTransport
30
+
31
+ _DEFAULT_GAE_LOGGER_NAME = "app"
32
+
33
+ _GAE_PROJECT_ENV_FLEX = "GCLOUD_PROJECT"
34
+ _GAE_PROJECT_ENV_STANDARD = "GOOGLE_CLOUD_PROJECT"
35
+ _GAE_SERVICE_ENV = "GAE_SERVICE"
36
+ _GAE_VERSION_ENV = "GAE_VERSION"
37
+
38
+ _TRACE_ID_LABEL = "appengine.googleapis.com/trace_id"
39
+
40
+ _DEPRECATION_MSG = "AppEngineHandler is deprecated. Use CloudLoggingHandler instead."
41
+
42
+
43
+ class AppEngineHandler(logging.StreamHandler):
44
+ """A logging handler that sends App Engine-formatted logs to Stackdriver.
45
+
46
+ DEPRECATED: use CloudLoggingHandler instead.
47
+ """
48
+
49
+ def __init__(
50
+ self,
51
+ client,
52
+ *,
53
+ name=_DEFAULT_GAE_LOGGER_NAME,
54
+ transport=BackgroundThreadTransport,
55
+ stream=None,
56
+ ):
57
+ """
58
+ Args:
59
+ client (~logging_v2.client.Client): The authenticated
60
+ Google Cloud Logging client for this handler to use.
61
+ name (Optional[str]): Name for the logger.
62
+ transport (Optional[~logging_v2.transports.Transport]):
63
+ The transport class. It should be a subclass
64
+ of :class:`.Transport`. If unspecified,
65
+ :class:`.BackgroundThreadTransport` will be used.
66
+ stream (Optional[IO]): Stream to be used by the handler.
67
+
68
+ """
69
+ super(AppEngineHandler, self).__init__(stream)
70
+ self.name = name
71
+ self.client = client
72
+ self.transport = transport(client, name)
73
+ self.project_id = os.environ.get(
74
+ _GAE_PROJECT_ENV_FLEX, os.environ.get(_GAE_PROJECT_ENV_STANDARD, "")
75
+ )
76
+ self.module_id = os.environ.get(_GAE_SERVICE_ENV, "")
77
+ self.version_id = os.environ.get(_GAE_VERSION_ENV, "")
78
+ self.resource = self.get_gae_resource()
79
+
80
+ warnings.warn(_DEPRECATION_MSG, DeprecationWarning)
81
+
82
+ def get_gae_resource(self):
83
+ """Return the GAE resource using the environment variables.
84
+
85
+ Returns:
86
+ google.cloud.logging_v2.resource.Resource: Monitored resource for GAE.
87
+ """
88
+ return _create_app_engine_resource()
89
+
90
+ def get_gae_labels(self):
91
+ """Return the labels for GAE app.
92
+
93
+ If the trace ID can be detected, it will be included as a label.
94
+ Currently, no other labels are included.
95
+
96
+ Returns:
97
+ dict: Labels for GAE app.
98
+ """
99
+ gae_labels = {}
100
+
101
+ _, trace_id, _, _ = get_request_data()
102
+ if trace_id is not None:
103
+ gae_labels[_TRACE_ID_LABEL] = trace_id
104
+
105
+ return gae_labels
106
+
107
+ def emit(self, record):
108
+ """Actually log the specified logging record.
109
+
110
+ Overrides the default emit behavior of ``StreamHandler``.
111
+
112
+ See https://docs.python.org/2/library/logging.html#handler-objects
113
+
114
+ Args:
115
+ record (logging.LogRecord): The record to be logged.
116
+ """
117
+ message = super(AppEngineHandler, self).format(record)
118
+ inferred_http, inferred_trace, _, _ = get_request_data()
119
+ if inferred_trace is not None:
120
+ inferred_trace = f"projects/{self.project_id}/traces/{inferred_trace}"
121
+ # allow user overrides
122
+ trace = getattr(record, "trace", inferred_trace)
123
+ span_id = getattr(record, "span_id", None)
124
+ http_request = getattr(record, "http_request", inferred_http)
125
+ resource = getattr(record, "resource", self.resource)
126
+ user_labels = getattr(record, "labels", {})
127
+ # merge labels
128
+ gae_labels = self.get_gae_labels()
129
+ gae_labels.update(user_labels)
130
+ # send off request
131
+ self.transport.send(
132
+ record,
133
+ message,
134
+ resource=resource,
135
+ labels=gae_labels,
136
+ trace=trace,
137
+ span_id=span_id,
138
+ http_request=http_request,
139
+ )
@@ -0,0 +1,62 @@
1
+ # Copyright 2016 Google LLC All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Logging handler for Google Container Engine (GKE).
16
+
17
+ Formats log messages in a JSON format, so that Kubernetes clusters with the
18
+ fluentd Google Cloud plugin installed can format their log messages so that
19
+ metadata such as log level is properly captured.
20
+ """
21
+
22
+ import logging.handlers
23
+ import warnings
24
+
25
+ from google.cloud.logging_v2.handlers._helpers import format_stackdriver_json
26
+
27
+ _DEPRECATION_MSG = (
28
+ "ContainerEngineHandler is deprecated. Use StructuredLogHandler instead."
29
+ )
30
+
31
+
32
+ class ContainerEngineHandler(logging.StreamHandler):
33
+ """Handler to format log messages the format expected by GKE fluent.
34
+
35
+ This handler is written to format messages for the Google Container Engine
36
+ (GKE) fluentd plugin, so that metadata such as log level are properly set.
37
+
38
+ DEPRECATED: use StructuredLogHandler to write formatted logs to standard out instead.
39
+ """
40
+
41
+ def __init__(self, *, name=None, stream=None):
42
+ """
43
+ Args:
44
+ name (Optional[str]): The name of the custom log in Cloud Logging.
45
+ stream (Optional[IO]): Stream to be used by the handler.
46
+
47
+ """
48
+ super(ContainerEngineHandler, self).__init__(stream=stream)
49
+ self.name = name
50
+ warnings.warn(_DEPRECATION_MSG, DeprecationWarning)
51
+
52
+ def format(self, record):
53
+ """Format the message into JSON expected by fluentd.
54
+
55
+ Args:
56
+ record (logging.LogRecord): The log record.
57
+
58
+ Returns:
59
+ str: A JSON string formatted for GKE fluentd.
60
+ """
61
+ message = super(ContainerEngineHandler, self).format(record)
62
+ return format_stackdriver_json(record, message)
@@ -0,0 +1,340 @@
1
+ # Copyright 2016 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Python :mod:`logging` handlers for Cloud Logging."""
16
+
17
+ import collections
18
+ import json
19
+ import logging
20
+
21
+ from typing import Optional, IO, Type
22
+
23
+ from google.cloud.logging_v2.handlers.transports import (
24
+ BackgroundThreadTransport,
25
+ Transport,
26
+ )
27
+ from google.cloud.logging_v2.handlers._monitored_resources import (
28
+ detect_resource,
29
+ add_resource_labels,
30
+ )
31
+ from google.cloud.logging_v2.handlers._helpers import get_request_data
32
+ from google.cloud.logging_v2.resource import Resource
33
+
34
+
35
+ DEFAULT_LOGGER_NAME = "python"
36
+
37
+ """Defaults for filtering out noisy loggers"""
38
+ EXCLUDED_LOGGER_DEFAULTS = (
39
+ "google.api_core.bidi",
40
+ "werkzeug",
41
+ )
42
+
43
+ """Exclude internal logs from propagating through handlers"""
44
+ _INTERNAL_LOGGERS = (
45
+ "google.cloud",
46
+ "google.auth",
47
+ "google_auth_httplib2",
48
+ )
49
+
50
+ """These environments require us to remove extra handlers on setup"""
51
+ _CLEAR_HANDLER_RESOURCE_TYPES = ("gae_app", "cloud_function")
52
+
53
+
54
+ class CloudLoggingFilter(logging.Filter):
55
+ """Python standard ``logging`` Filter class to add Cloud Logging
56
+ information to each LogRecord.
57
+
58
+ When attached to a LogHandler, each incoming log will be modified
59
+ to include new Cloud Logging relevant data. This data can be manually
60
+ overwritten using the `extras` argument when writing logs.
61
+ """
62
+
63
+ def __init__(self, project=None, default_labels=None):
64
+ self.project = project
65
+ self.default_labels = default_labels if default_labels else {}
66
+
67
+ @staticmethod
68
+ def _infer_source_location(record):
69
+ """Helper function to infer source location data from a LogRecord.
70
+ Will default to record.source_location if already set
71
+ """
72
+ if hasattr(record, "source_location"):
73
+ return record.source_location
74
+ else:
75
+ name_map = [
76
+ ("line", "lineno"),
77
+ ("file", "pathname"),
78
+ ("function", "funcName"),
79
+ ]
80
+ output = {}
81
+ for gcp_name, std_lib_name in name_map:
82
+ value = getattr(record, std_lib_name, None)
83
+ if value is not None:
84
+ output[gcp_name] = value
85
+ return output if output else None
86
+
87
+ def filter(self, record):
88
+ """
89
+ Add new Cloud Logging data to each LogRecord as it comes in
90
+ """
91
+ user_labels = getattr(record, "labels", {})
92
+ # infer request data from the environment
93
+ (
94
+ inferred_http,
95
+ inferred_trace,
96
+ inferred_span,
97
+ inferred_sampled,
98
+ ) = get_request_data()
99
+ if inferred_trace is not None and self.project is not None:
100
+ # add full path for detected trace
101
+ inferred_trace = f"projects/{self.project}/traces/{inferred_trace}"
102
+ # set new record values
103
+ record._resource = getattr(record, "resource", None)
104
+ record._trace = getattr(record, "trace", inferred_trace) or None
105
+ record._span_id = getattr(record, "span_id", inferred_span) or None
106
+ record._trace_sampled = bool(getattr(record, "trace_sampled", inferred_sampled))
107
+ record._http_request = getattr(record, "http_request", inferred_http)
108
+ record._source_location = CloudLoggingFilter._infer_source_location(record)
109
+ # add logger name as a label if possible
110
+ logger_label = {"python_logger": record.name} if record.name else {}
111
+ record._labels = {**logger_label, **self.default_labels, **user_labels} or None
112
+ # create string representations for structured logging
113
+ record._trace_str = record._trace or ""
114
+ record._span_id_str = record._span_id or ""
115
+ record._trace_sampled_str = "true" if record._trace_sampled else "false"
116
+ record._http_request_str = json.dumps(
117
+ record._http_request or {}, ensure_ascii=False
118
+ )
119
+ record._source_location_str = json.dumps(
120
+ record._source_location or {}, ensure_ascii=False
121
+ )
122
+ record._labels_str = json.dumps(record._labels or {}, ensure_ascii=False)
123
+ return True
124
+
125
+
126
+ class CloudLoggingHandler(logging.StreamHandler):
127
+ """Handler that directly makes Cloud Logging API calls.
128
+
129
+ This is a Python standard ``logging`` handler using that can be used to
130
+ route Python standard logging messages directly to the Stackdriver
131
+ Logging API.
132
+
133
+ This handler is used when not in GAE or GKE environment.
134
+
135
+ This handler supports both an asynchronous and synchronous transport.
136
+
137
+ Example:
138
+
139
+ .. code-block:: python
140
+
141
+ import logging
142
+ import google.cloud.logging
143
+ from google.cloud.logging_v2.handlers import CloudLoggingHandler
144
+
145
+ client = google.cloud.logging.Client()
146
+ handler = CloudLoggingHandler(client)
147
+
148
+ cloud_logger = logging.getLogger('cloudLogger')
149
+ cloud_logger.setLevel(logging.INFO)
150
+ cloud_logger.addHandler(handler)
151
+
152
+ cloud_logger.error('bad news') # API call
153
+ """
154
+
155
+ def __init__(
156
+ self,
157
+ client,
158
+ *,
159
+ name: str = DEFAULT_LOGGER_NAME,
160
+ transport: Type[Transport] = BackgroundThreadTransport,
161
+ resource: Resource = None,
162
+ labels: Optional[dict] = None,
163
+ stream: Optional[IO] = None,
164
+ **kwargs,
165
+ ):
166
+ """
167
+ Args:
168
+ client (~logging_v2.client.Client):
169
+ The authenticated Google Cloud Logging client for this
170
+ handler to use.
171
+ name (str): the name of the custom log in Cloud Logging.
172
+ Defaults to 'python'. The name of the Python logger will be represented
173
+ in the ``python_logger`` field.
174
+ transport (~logging_v2.transports.Transport):
175
+ Class for creating new transport objects. It should
176
+ extend from the base :class:`.Transport` type and
177
+ implement :meth`.Transport.send`. Defaults to
178
+ :class:`.BackgroundThreadTransport`. The other
179
+ option is :class:`.SyncTransport`.
180
+ resource (~logging_v2.resource.Resource):
181
+ Resource for this Handler. If not given, will be inferred from the environment.
182
+ labels (Optional[dict]): Additional labels to attach to logs.
183
+ stream (Optional[IO]): Stream to be used by the handler.
184
+ """
185
+ super(CloudLoggingHandler, self).__init__(stream)
186
+ if not resource:
187
+ # infer the correct monitored resource from the local environment
188
+ resource = detect_resource(client.project)
189
+ self.name = name
190
+ self.client = client
191
+ client._handlers.add(self)
192
+ self.transport = transport(client, name, resource=resource)
193
+ self._transport_open = True
194
+ self._transport_cls = transport
195
+ self.project_id = client.project
196
+ self.resource = resource
197
+ self.labels = labels
198
+ # add extra keys to log record
199
+ log_filter = CloudLoggingFilter(project=self.project_id, default_labels=labels)
200
+ self.addFilter(log_filter)
201
+
202
+ def emit(self, record):
203
+ """Actually log the specified logging record.
204
+
205
+ Overrides the default emit behavior of ``StreamHandler``.
206
+
207
+ See https://docs.python.org/2/library/logging.html#handler-objects
208
+
209
+ Args:
210
+ record (logging.LogRecord): The record to be logged.
211
+ """
212
+ resource = record._resource or self.resource
213
+ labels = record._labels
214
+ message = _format_and_parse_message(record, self)
215
+
216
+ labels = {**add_resource_labels(resource, record), **(labels or {})} or None
217
+
218
+ # send off request
219
+ if not self._transport_open:
220
+ self.transport = self._transport_cls(
221
+ self.client, self.name, resource=self.resource
222
+ )
223
+ self._transport_open = True
224
+
225
+ self.transport.send(
226
+ record,
227
+ message,
228
+ resource=resource,
229
+ labels=labels,
230
+ trace=record._trace,
231
+ span_id=record._span_id,
232
+ trace_sampled=record._trace_sampled,
233
+ http_request=record._http_request,
234
+ source_location=record._source_location,
235
+ )
236
+
237
+ def flush(self):
238
+ """Forces the Transport object to submit any pending log records.
239
+
240
+ For SyncTransport, this is a no-op.
241
+ """
242
+ super(CloudLoggingHandler, self).flush()
243
+ if self._transport_open:
244
+ self.transport.flush()
245
+
246
+ def close(self):
247
+ """Closes the log handler and cleans up all Transport objects used."""
248
+ if self._transport_open:
249
+ self.transport.close()
250
+ self.transport = None
251
+ self._transport_open = False
252
+
253
+
254
+ def _format_and_parse_message(record, formatter_handler):
255
+ """
256
+ Helper function to apply formatting to a LogRecord message,
257
+ and attempt to parse encoded JSON into a dictionary object.
258
+
259
+ Resulting output will be of type (str | dict | None)
260
+
261
+ Args:
262
+ record (logging.LogRecord): The record object representing the log
263
+ formatter_handler (logging.Handler): The handler used to format the log
264
+ """
265
+ passed_json_fields = getattr(record, "json_fields", {})
266
+ # if message is a dictionary, use dictionary directly
267
+ if isinstance(record.msg, collections.abc.Mapping):
268
+ payload = record.msg
269
+ # attach any extra json fields if present
270
+ if passed_json_fields and isinstance(
271
+ passed_json_fields, collections.abc.Mapping
272
+ ):
273
+ payload = {**payload, **passed_json_fields}
274
+ return payload
275
+ # format message string based on superclass
276
+ message = formatter_handler.format(record)
277
+ try:
278
+ # attempt to parse encoded json into dictionary
279
+ if message[0] == "{":
280
+ json_message = json.loads(message)
281
+ if isinstance(json_message, collections.abc.Mapping):
282
+ message = json_message
283
+ except (json.decoder.JSONDecodeError, IndexError):
284
+ # log string is not valid json
285
+ pass
286
+ # if json_fields was set, create a dictionary using that
287
+ if passed_json_fields and isinstance(passed_json_fields, collections.abc.Mapping):
288
+ passed_json_fields = passed_json_fields.copy()
289
+ if message != "None":
290
+ passed_json_fields["message"] = message
291
+ return passed_json_fields
292
+ # if formatted message contains no content, return None
293
+ return message if message != "None" else None
294
+
295
+
296
+ def setup_logging(
297
+ handler, *, excluded_loggers=EXCLUDED_LOGGER_DEFAULTS, log_level=logging.INFO
298
+ ):
299
+ """Attach a logging handler to the Python root logger
300
+
301
+ Excludes loggers that this library itself uses to avoid
302
+ infinite recursion.
303
+
304
+ Example:
305
+
306
+ .. code-block:: python
307
+
308
+ import logging
309
+ import google.cloud.logging
310
+ from google.cloud.logging_v2.handlers import CloudLoggingHandler
311
+
312
+ client = google.cloud.logging.Client()
313
+ handler = CloudLoggingHandler(client)
314
+ google.cloud.logging.handlers.setup_logging(handler)
315
+ logging.getLogger().setLevel(logging.DEBUG)
316
+
317
+ logging.error('bad news') # API call
318
+
319
+ Args:
320
+ handler (logging.handler): the handler to attach to the global handler
321
+ excluded_loggers (Optional[Tuple[str]]): The loggers to not attach the handler
322
+ to. This will always include the loggers in the
323
+ path of the logging client itself.
324
+ log_level (Optional[int]): The logging level threshold of the attached logger,
325
+ as set by the :meth:`logging.Logger.setLevel` method. Defaults to
326
+ :const:`logging.INFO`.
327
+ """
328
+ all_excluded_loggers = set(excluded_loggers + _INTERNAL_LOGGERS)
329
+ logger = logging.getLogger()
330
+
331
+ # remove built-in handlers on App Engine or Cloud Functions environments
332
+ if detect_resource().type in _CLEAR_HANDLER_RESOURCE_TYPES:
333
+ logger.handlers.clear()
334
+
335
+ logger.setLevel(log_level)
336
+ logger.addHandler(handler)
337
+ for logger_name in all_excluded_loggers:
338
+ # prevent excluded loggers from propagating logs to handler
339
+ logger = logging.getLogger(logger_name)
340
+ logger.propagate = False
@@ -0,0 +1,17 @@
1
+ # Copyright 2017 Google LLC All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from google.cloud.logging_v2.handlers.middleware.request import RequestMiddleware
16
+
17
+ __all__ = ["RequestMiddleware"]
@@ -0,0 +1,52 @@
1
+ # Copyright 2017 Google LLC All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Django middleware helper to capture a request.
16
+
17
+ The request is stored on a thread-local so that it can be
18
+ inspected by other helpers.
19
+ """
20
+
21
+ import threading
22
+
23
+
24
+ _thread_locals = threading.local()
25
+
26
+
27
+ def _get_django_request():
28
+ """Get Django request from thread local.
29
+
30
+ Returns:
31
+ str: Django request
32
+ """
33
+ return getattr(_thread_locals, "request", None)
34
+
35
+
36
+ def RequestMiddleware(get_response):
37
+ """Saves the request in thread local"""
38
+
39
+ def middleware(request):
40
+ """Called on each request, before Django decides which view to execute.
41
+
42
+ Args:
43
+ request(django.http.request.HttpRequest):
44
+ Django http request.
45
+ """
46
+ _thread_locals.request = request
47
+ if get_response:
48
+ return get_response(request)
49
+ else:
50
+ return None
51
+
52
+ return middleware