Skip to content

Commit

Permalink
fix: Added placeholder kwargs to StructuredLogHandler (#845)
Browse files Browse the repository at this point in the history
* fix: Added placeholder kwargs to StructuredLogHandler

* Replaced unused named arguments with **kwargs

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* linting

* Update structured_log.py

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
gkevinzheng and gcf-owl-bot[bot] committed Feb 1, 2024
1 parent a461ac0 commit 9bc0a37
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions google/cloud/logging_v2/handlers/handlers.py
Expand Up @@ -157,6 +157,7 @@ def __init__(
resource=None,
labels=None,
stream=None,
**kwargs,
):
"""
Args:
Expand Down
8 changes: 7 additions & 1 deletion google/cloud/logging_v2/handlers/structured_log.py
Expand Up @@ -63,7 +63,13 @@ class StructuredLogHandler(logging.StreamHandler):
"""

def __init__(
self, *, labels=None, stream=None, project_id=None, json_encoder_cls=None
self,
*,
labels=None,
stream=None,
project_id=None,
json_encoder_cls=None,
**kwargs
):
"""
Args:
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_client.py
Expand Up @@ -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
Expand Down

0 comments on commit 9bc0a37

Please sign in to comment.