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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add option to omit logging.StreamHandler during setup #104

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 15 additions & 2 deletions google/cloud/logging_v2/client.py
Expand Up @@ -361,7 +361,12 @@ def get_default_handler(self, **kw):
return CloudLoggingHandler(self, **kw)

def setup_logging(
self, *, log_level=logging.INFO, excluded_loggers=EXCLUDED_LOGGER_DEFAULTS, **kw
self,
*,
log_level=logging.INFO,
excluded_loggers=EXCLUDED_LOGGER_DEFAULTS,
stream_handler=True,
**kw
):
"""Attach default Cloud Logging handler to the root logger.

Expand All @@ -377,8 +382,16 @@ def setup_logging(
handler to. This will always include the
loggers in the path of the logging client
itself.
stream_handler (Optional[bool]): If True, a :const:`logging.StreamHandler`
is added in addition to the default Cloud Logging handler.
Defaults to :const:`True`.
Returns:
dict: keyword args passed to handler constructor
"""
handler = self.get_default_handler(**kw)
setup_logging(handler, log_level=log_level, excluded_loggers=excluded_loggers)
setup_logging(
handler,
log_level=log_level,
excluded_loggers=excluded_loggers,
stream_handler=stream_handler
)
11 changes: 9 additions & 2 deletions google/cloud/logging_v2/handlers/handlers.py
Expand Up @@ -105,7 +105,11 @@ def emit(self, record):


def setup_logging(
handler, *, excluded_loggers=EXCLUDED_LOGGER_DEFAULTS, log_level=logging.INFO
handler,
*,
excluded_loggers=EXCLUDED_LOGGER_DEFAULTS,
log_level=logging.INFO,
stream_handler=True
):
"""Attach a logging handler to the Python root logger

Expand Down Expand Up @@ -134,12 +138,15 @@ def setup_logging(
path of the logging client itself.
log_level (Optional[int]): Python logging log level. Defaults to
:const:`logging.INFO`.
stream_handler (Optional[bool]): If True, a :const:`logging.StreamHandler`
is added in addition to the handler. Defaults to :const:`True`.
"""
all_excluded_loggers = set(excluded_loggers + EXCLUDED_LOGGER_DEFAULTS)
logger = logging.getLogger()
logger.setLevel(log_level)
logger.addHandler(handler)
logger.addHandler(logging.StreamHandler())
if stream_handler:
logger.addHandler(logging.StreamHandler())
for logger_name in all_excluded_loggers:
logger = logging.getLogger(logger_name)
logger.propagate = False
Expand Down
20 changes: 17 additions & 3 deletions tests/unit/handlers/test_handlers.py
Expand Up @@ -90,13 +90,15 @@ def test_emit(self):


class TestSetupLogging(unittest.TestCase):
def _call_fut(self, handler, excludes=None):
def _call_fut(self, handler, excludes=None, stream_handler=True):
from google.cloud.logging_v2.handlers.handlers import setup_logging

if excludes:
return setup_logging(handler, excluded_loggers=excludes)
return setup_logging(
handler, excluded_loggers=excludes, stream_handler=stream_handler
)
else:
return setup_logging(handler)
return setup_logging(handler, stream_handler=stream_handler)

def test_setup_logging(self):
handler = _Handler(logging.INFO)
Expand All @@ -105,6 +107,18 @@ def test_setup_logging(self):
root_handlers = logging.getLogger().handlers
self.assertIn(handler, root_handlers)

def test_setup_logging_no_stream_handler(self):
# Remove all handlers to be able to check
# the list after call setup_logging
logging.getLogger().handlers = []

handler = _Handler(logging.INFO)
self._call_fut(handler, stream_handler=False)

root_handlers = logging.getLogger().handlers
self.assertEqual(len(root_handlers), 1)
self.assertEqual(handler, root_handlers[0])

def test_setup_logging_excludes(self):
INCLUDED_LOGGER_NAME = "includeme"
EXCLUDED_LOGGER_NAME = "excludeme"
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_client.py
Expand Up @@ -799,6 +799,7 @@ def test_setup_logging(self):
expected_kwargs = {
"excluded_loggers": ("google.cloud", "google.auth", "google_auth_httplib2"),
"log_level": 20,
"stream_handler": True,
}
self.assertEqual(kwargs, expected_kwargs)

Expand Down Expand Up @@ -836,6 +837,7 @@ def test_setup_logging_w_extra_kwargs(self):
expected_kwargs = {
"excluded_loggers": ("google.cloud", "google.auth", "google_auth_httplib2"),
"log_level": 20,
"stream_handler": True,
}
self.assertEqual(kwargs, expected_kwargs)

Expand Down