Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d305a2e

Browse filesBrowse files
feat!: Infer default resource in logger (#315)
1 parent 2b4ef6f commit d305a2e
Copy full SHA for d305a2e

File tree

Expand file treeCollapse file tree

12 files changed

+115
-35
lines changed
Filter options
Expand file treeCollapse file tree

12 files changed

+115
-35
lines changed

‎.github/.OwlBot.lock.yaml

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
docker:
22
image: gcr.io/repo-automation-bots/owlbot-python:latest
3-
digest: sha256:c66ba3c8d7bc8566f47df841f98cd0097b28fff0b1864c86f5817f4c8c3e8600
3+
digest: sha256:58c7342b0bccf85028100adaa3d856cb4a871c22ca9c01960d996e66c40548ce

‎CHANGELOG.md

Copy file name to clipboardExpand all lines: CHANGELOG.md
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
[1]: https://pypi.org/project/google-cloud-logging/#history
66

7+
## [2.5.0](https://www.github.com/googleapis/python-logging/compare/v2.4.0...v2.5.0) (2021-06-10)
8+
9+
10+
### Features
11+
12+
* support AuditLog and RequestLog protos ([#274](https://www.github.com/googleapis/python-logging/issues/274)) ([5d91be9](https://www.github.com/googleapis/python-logging/commit/5d91be9f121c364cbd53c6a9fffc4fb6ca6bd324))
13+
14+
15+
### Bug Fixes
16+
17+
* **deps:** add packaging requirement ([#300](https://www.github.com/googleapis/python-logging/issues/300)) ([68c5cec](https://www.github.com/googleapis/python-logging/commit/68c5ceced3288253af8e3c6013a35fa3954b37bc))
18+
* structured log handler formatting issues ([#319](https://www.github.com/googleapis/python-logging/issues/319)) ([db9da37](https://www.github.com/googleapis/python-logging/commit/db9da3700511b5a24c3c44c9f4377705937caf46))
19+
720
## [2.4.0](https://www.github.com/googleapis/python-logging/compare/v2.3.1...v2.4.0) (2021-05-12)
821

922

‎docs/conf.py

Copy file name to clipboardExpand all lines: docs/conf.py
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080
master_doc = "index"
8181

8282
# General information about the project.
83-
project = u"google-cloud-logging"
84-
copyright = u"2019, Google"
85-
author = u"Google APIs"
83+
project = "google-cloud-logging"
84+
copyright = "2019, Google"
85+
author = "Google APIs"
8686

8787
# The version info for the project you're documenting, acts as replacement for
8888
# |version| and |release|, also used in various other places throughout the
@@ -281,7 +281,7 @@
281281
(
282282
master_doc,
283283
"google-cloud-logging.tex",
284-
u"google-cloud-logging Documentation",
284+
"google-cloud-logging Documentation",
285285
author,
286286
"manual",
287287
)
@@ -316,7 +316,7 @@
316316
(
317317
master_doc,
318318
"google-cloud-logging",
319-
u"google-cloud-logging Documentation",
319+
"google-cloud-logging Documentation",
320320
[author],
321321
1,
322322
)
@@ -335,7 +335,7 @@
335335
(
336336
master_doc,
337337
"google-cloud-logging",
338-
u"google-cloud-logging Documentation",
338+
"google-cloud-logging Documentation",
339339
author,
340340
"google-cloud-logging",
341341
"google-cloud-logging Library",

‎google/cloud/logging_v2/client.py

Copy file name to clipboardExpand all lines: google/cloud/logging_v2/client.py
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,21 @@ def metrics_api(self):
182182
self._metrics_api = JSONMetricsAPI(self)
183183
return self._metrics_api
184184

185-
def logger(self, name):
185+
def logger(self, name, *, labels=None, resource=None):
186186
"""Creates a logger bound to the current client.
187187
188188
Args:
189189
name (str): The name of the logger to be constructed.
190+
resource (Optional[~logging_v2.Resource]): a monitored resource object
191+
representing the resource the code was run on. If not given, will
192+
be inferred from the environment.
193+
labels (Optional[dict]): Mapping of default labels for entries written
194+
via this logger.
190195
191196
Returns:
192197
~logging_v2.logger.Logger: Logger created with the current client.
193198
"""
194-
return Logger(name, client=self)
199+
return Logger(name, client=self, labels=labels, resource=resource)
195200

196201
def list_entries(
197202
self,

‎google/cloud/logging_v2/handlers/handlers.py

Copy file name to clipboardExpand all lines: google/cloud/logging_v2/handlers/handlers.py
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import json
1919
import logging
2020

21-
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
2221
from google.cloud.logging_v2.handlers.transports import BackgroundThreadTransport
2322
from google.cloud.logging_v2.handlers._monitored_resources import detect_resource
2423
from google.cloud.logging_v2.handlers._helpers import get_request_data
@@ -144,7 +143,7 @@ def __init__(
144143
*,
145144
name=DEFAULT_LOGGER_NAME,
146145
transport=BackgroundThreadTransport,
147-
resource=_GLOBAL_RESOURCE,
146+
resource=None,
148147
labels=None,
149148
stream=None,
150149
):
@@ -163,11 +162,14 @@ def __init__(
163162
:class:`.BackgroundThreadTransport`. The other
164163
option is :class:`.SyncTransport`.
165164
resource (~logging_v2.resource.Resource):
166-
Resource for this Handler. Defaults to ``global``.
165+
Resource for this Handler. If not given, will be inferred from the environment.
167166
labels (Optional[dict]): Additional labels to attach to logs.
168167
stream (Optional[IO]): Stream to be used by the handler.
169168
"""
170169
super(CloudLoggingHandler, self).__init__(stream)
170+
if not resource:
171+
# infer the correct monitored resource from the local environment
172+
resource = detect_resource(client.project)
171173
self.name = name
172174
self.client = client
173175
self.transport = transport(client, name)

‎google/cloud/logging_v2/logger.py

Copy file name to clipboardExpand all lines: google/cloud/logging_v2/logger.py
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from google.cloud.logging_v2.entries import StructEntry
2323
from google.cloud.logging_v2.entries import TextEntry
2424
from google.cloud.logging_v2.resource import Resource
25+
from google.cloud.logging_v2.handlers._monitored_resources import detect_resource
2526

2627
import google.protobuf.message
2728

@@ -51,19 +52,23 @@ class Logger(object):
5152
See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs
5253
"""
5354

54-
def __init__(self, name, client, *, labels=None, resource=_GLOBAL_RESOURCE):
55+
def __init__(self, name, client, *, labels=None, resource=None):
5556
"""
5657
Args:
5758
name (str): The name of the logger.
5859
client (~logging_v2.client.Client):
5960
A client which holds credentials and project configuration
6061
for the logger (which requires a project).
61-
resource (~logging_v2.Resource): a monitored resource object
62-
representing the resource the code was run on.
62+
resource (Optional[~logging_v2.Resource]): a monitored resource object
63+
representing the resource the code was run on. If not given, will
64+
be inferred from the environment.
6365
labels (Optional[dict]): Mapping of default labels for entries written
6466
via this logger.
6567
6668
"""
69+
if not resource:
70+
# infer the correct monitored resource from the local environment
71+
resource = detect_resource(client.project)
6772
self.name = name
6873
self._client = client
6974
self.labels = labels

‎samples/snippets/usage_guide.py

Copy file name to clipboardExpand all lines: samples/snippets/usage_guide.py
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,19 @@ def _sink_pubsub_setup(client):
264264
) # API call
265265
# [END sink_topic_permissions]
266266

267-
return topic
267+
# create callback wrapper to delete topic when done
268+
class TopicDeleter:
269+
def delete(self):
270+
client.delete_topic(request={"topic": topic_path})
271+
272+
return topic, TopicDeleter()
268273

269274

270275
@snippet
271276
def sink_pubsub(client, to_delete):
272277
"""Sink log entries to pubsub."""
273-
topic = _sink_pubsub_setup(client)
278+
topic, topic_deleter = _sink_pubsub_setup(client)
279+
to_delete.append(topic_deleter)
274280
sink_name = "robots-pubsub-%d" % (_millis(),)
275281
filter_str = "logName:apache-access AND textPayload:robot"
276282
updated_filter = "textPayload:robot"
@@ -282,6 +288,7 @@ def sink_pubsub(client, to_delete):
282288
sink.create() # API call
283289
assert sink.exists() # API call
284290
# [END sink_pubsub_create]
291+
to_delete.append(sink)
285292
created_sink = sink
286293

287294
# [START client_list_sinks]

‎setup.py

Copy file name to clipboardExpand all lines: setup.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
name = "google-cloud-logging"
2424
description = "Stackdriver Logging API client library"
25-
version = "2.4.0"
25+
version = "2.5.0"
2626
# Should be one of:
2727
# 'Development Status :: 3 - Alpha'
2828
# 'Development Status :: 4 - Beta'

‎tests/unit/handlers/test_handlers.py

Copy file name to clipboardExpand all lines: tests/unit/handlers/test_handlers.py
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ def _make_one(self, *args, **kw):
236236

237237
def test_ctor_defaults(self):
238238
import sys
239-
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
239+
from google.cloud.logging_v2.handlers._monitored_resources import (
240+
_create_global_resource,
241+
)
240242
from google.cloud.logging_v2.handlers.handlers import DEFAULT_LOGGER_NAME
241243

242244
patch = mock.patch(
@@ -251,7 +253,8 @@ def test_ctor_defaults(self):
251253
self.assertIsInstance(handler.transport, _Transport)
252254
self.assertIs(handler.transport.client, client)
253255
self.assertEqual(handler.transport.name, DEFAULT_LOGGER_NAME)
254-
self.assertEqual(handler.resource, _GLOBAL_RESOURCE)
256+
global_resource = _create_global_resource(self.PROJECT)
257+
self.assertEqual(handler.resource, global_resource)
255258
self.assertIsNone(handler.labels)
256259
self.assertIs(handler.stream, sys.stderr)
257260

‎tests/unit/test_client.py

Copy file name to clipboardExpand all lines: tests/unit/test_client.py
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,20 @@ def make_api(client_obj):
239239

240240
def test_logger(self):
241241
from google.cloud.logging import Logger
242+
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
242243

243244
creds = _make_credentials()
244245
client = self._make_one(project=self.PROJECT, credentials=creds)
245-
logger = client.logger(self.LOGGER_NAME)
246+
labels = {"test": "true"}
247+
logger = client.logger(
248+
self.LOGGER_NAME, resource=_GLOBAL_RESOURCE, labels=labels
249+
)
246250
self.assertIsInstance(logger, Logger)
247251
self.assertEqual(logger.name, self.LOGGER_NAME)
248252
self.assertIs(logger.client, client)
249253
self.assertEqual(logger.project, self.PROJECT)
254+
self.assertEqual(logger.default_resource, _GLOBAL_RESOURCE)
255+
self.assertEqual(logger.labels, labels)
250256

251257
def test_list_entries_defaults(self):
252258
from google.cloud.logging import TextEntry

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.