From 5a17778e3b945d05947bbb894847ce7afe6692b3 Mon Sep 17 00:00:00 2001 From: Kevin Zheng Date: Mon, 29 Jan 2024 22:28:34 +0000 Subject: [PATCH 1/5] fix: Added placeholder kwargs to StructuredLogHandler --- .../logging_v2/handlers/structured_log.py | 12 ++++++- tests/unit/test_client.py | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/google/cloud/logging_v2/handlers/structured_log.py b/google/cloud/logging_v2/handlers/structured_log.py index e6094091e..3cd68b4cb 100644 --- a/google/cloud/logging_v2/handlers/structured_log.py +++ b/google/cloud/logging_v2/handlers/structured_log.py @@ -20,6 +20,7 @@ import logging.handlers from google.cloud.logging_v2.handlers.handlers import CloudLoggingFilter +from google.cloud.logging_v2.handlers.handlers import DEFAULT_LOGGER_NAME from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message import google.cloud.logging_v2 from google.cloud.logging_v2._instrumentation import _create_diagnostic_entry @@ -63,10 +64,19 @@ class StructuredLogHandler(logging.StreamHandler): """ def __init__( - self, *, labels=None, stream=None, project_id=None, json_encoder_cls=None + self, + *, + name=DEFAULT_LOGGER_NAME, + resource=None, + labels=None, + stream=None, + project_id=None, + json_encoder_cls=None ): """ Args: + name (Optional[str]): Placeholder for setup_logging consistency. Does nothing. + resource (Optional[dict]): Placeholder for setup_logging consistency. Does nothing. labels (Optional[dict]): Additional labels to attach to logs. stream (Optional[IO]): Stream to be used by the handler. project (Optional[str]): Project Id associated with the logs. diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 2f6736dcf..2d12a283e 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -894,6 +894,42 @@ def test_setup_logging_w_extra_kwargs(self): } self.assertEqual(kwargs, expected_kwargs) + def test_setup_logging_w_extra_kwargs_structured_log(self): + import io + from google.cloud.logging.handlers import StructuredLogHandler + from google.cloud.logging import Resource + from google.cloud.logging_v2.client import _GKE_RESOURCE_TYPE + + name = "test-logger" + resource = Resource(_GKE_RESOURCE_TYPE, {"resource_label": "value"}) + labels = {"handler_label": "value"} + stream = io.BytesIO() + + credentials = _make_credentials() + client = self._make_one( + project=self.PROJECT, credentials=credentials, _use_grpc=False + ) + + with mock.patch("google.cloud.logging_v2.client.setup_logging") as mocked: + client.setup_logging( + name=name, resource=resource, labels=labels, stream=stream + ) + + self.assertEqual(len(mocked.mock_calls), 1) + _, args, kwargs = mocked.mock_calls[0] + + (handler,) = args + self.assertIsInstance(handler, StructuredLogHandler) + + expected_kwargs = { + "excluded_loggers": ( + "google.api_core.bidi", + "werkzeug", + ), + "log_level": 20, + } + self.assertEqual(kwargs, expected_kwargs) + class _Connection(object): _called_with = None From fcc53e68ad57051f4d3951a7bb8d43d714ea49b6 Mon Sep 17 00:00:00 2001 From: Kevin Zheng Date: Tue, 30 Jan 2024 16:01:04 +0000 Subject: [PATCH 2/5] Replaced unused named arguments with **kwargs --- google/cloud/logging_v2/handlers/handlers.py | 1 + google/cloud/logging_v2/handlers/structured_log.py | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/google/cloud/logging_v2/handlers/handlers.py b/google/cloud/logging_v2/handlers/handlers.py index 34bb018d8..4e5fda68a 100644 --- a/google/cloud/logging_v2/handlers/handlers.py +++ b/google/cloud/logging_v2/handlers/handlers.py @@ -157,6 +157,7 @@ def __init__( resource=None, labels=None, stream=None, + **kwargs ): """ Args: diff --git a/google/cloud/logging_v2/handlers/structured_log.py b/google/cloud/logging_v2/handlers/structured_log.py index 3cd68b4cb..24cc7274b 100644 --- a/google/cloud/logging_v2/handlers/structured_log.py +++ b/google/cloud/logging_v2/handlers/structured_log.py @@ -66,12 +66,11 @@ class StructuredLogHandler(logging.StreamHandler): def __init__( self, *, - name=DEFAULT_LOGGER_NAME, - resource=None, labels=None, stream=None, project_id=None, - json_encoder_cls=None + json_encoder_cls=None, + **kwargs ): """ Args: From b8c15e3bd73b213159f7fee6c361a7514191c84b Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 30 Jan 2024 16:03:32 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- google/cloud/logging_v2/handlers/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/cloud/logging_v2/handlers/handlers.py b/google/cloud/logging_v2/handlers/handlers.py index 4e5fda68a..3d6ab9d1e 100644 --- a/google/cloud/logging_v2/handlers/handlers.py +++ b/google/cloud/logging_v2/handlers/handlers.py @@ -157,7 +157,7 @@ def __init__( resource=None, labels=None, stream=None, - **kwargs + **kwargs, ): """ Args: From 7533384be1777ec5ce814148f44ae0e41090c873 Mon Sep 17 00:00:00 2001 From: Kevin Zheng Date: Tue, 30 Jan 2024 17:08:25 +0000 Subject: [PATCH 4/5] linting --- google/cloud/logging_v2/handlers/structured_log.py | 1 - 1 file changed, 1 deletion(-) diff --git a/google/cloud/logging_v2/handlers/structured_log.py b/google/cloud/logging_v2/handlers/structured_log.py index 24cc7274b..72cb9112c 100644 --- a/google/cloud/logging_v2/handlers/structured_log.py +++ b/google/cloud/logging_v2/handlers/structured_log.py @@ -20,7 +20,6 @@ import logging.handlers from google.cloud.logging_v2.handlers.handlers import CloudLoggingFilter -from google.cloud.logging_v2.handlers.handlers import DEFAULT_LOGGER_NAME from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message import google.cloud.logging_v2 from google.cloud.logging_v2._instrumentation import _create_diagnostic_entry From af68de2b70195e4797b29cce0d8ca87d9bc9684e Mon Sep 17 00:00:00 2001 From: Kevin Zheng <147537668+gkevinzheng@users.noreply.github.com> Date: Wed, 31 Jan 2024 13:27:51 -0500 Subject: [PATCH 5/5] Update structured_log.py --- google/cloud/logging_v2/handlers/structured_log.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/google/cloud/logging_v2/handlers/structured_log.py b/google/cloud/logging_v2/handlers/structured_log.py index 72cb9112c..dcba02c9c 100644 --- a/google/cloud/logging_v2/handlers/structured_log.py +++ b/google/cloud/logging_v2/handlers/structured_log.py @@ -73,8 +73,6 @@ def __init__( ): """ Args: - name (Optional[str]): Placeholder for setup_logging consistency. Does nothing. - resource (Optional[dict]): Placeholder for setup_logging consistency. Does nothing. labels (Optional[dict]): Additional labels to attach to logs. stream (Optional[IO]): Stream to be used by the handler. project (Optional[str]): Project Id associated with the logs.