Skip to content
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

fix: Added placeholder kwargs to StructuredLogHandler #845

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
10 changes: 9 additions & 1 deletion google/cloud/logging_v2/handlers/structured_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ 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:
name (Optional[str]): Placeholder for setup_logging consistency. Does nothing.
resource (Optional[dict]): Placeholder for setup_logging consistency. Does nothing.
daniel-sanche marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can probably remove these comments about the args.

Maybe add a line for **kwargs to the Args list here if you want to mention that others are ignored, but that's optional

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.
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