Skip to content

fix: Added placeholder kwargs to StructuredLogHandler #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions google/cloud/logging_v2/handlers/handlers.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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