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: Allowed for a partial override of loggers that get excluded from setup_client #831

Merged
merged 4 commits into from
Jan 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions google/cloud/logging_v2/handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@

DEFAULT_LOGGER_NAME = "python"

"""Exclude internal logs from propagating through handlers"""
"""Defaults for filtering out noisy loggers"""
EXCLUDED_LOGGER_DEFAULTS = (
"google.api_core.bidi",
"werkzeug",
)

"""Exclude internal logs from propagating through handlers"""
_INTERNAL_LOGGERS = (
"google.cloud",
"google.auth",
"google_auth_httplib2",
"google.api_core.bidi",
"werkzeug",
)

"""These environments require us to remove extra handlers on setup"""
Expand Down Expand Up @@ -291,7 +295,7 @@ def setup_logging(
log_level (Optional[int]): Python logging log level. Defaults to
:const:`logging.INFO`.
"""
all_excluded_loggers = set(excluded_loggers + EXCLUDED_LOGGER_DEFAULTS)
all_excluded_loggers = set(excluded_loggers + _INTERNAL_LOGGERS)
logger = logging.getLogger()

# remove built-in handlers on App Engine or Cloud Functions environments
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/handlers/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import mock
import json

from google.cloud.logging_v2.handlers.handlers import (
_INTERNAL_LOGGERS,
EXCLUDED_LOGGER_DEFAULTS,
)

from google.cloud.logging_v2.handlers._monitored_resources import (
_FUNCTION_ENV_VARS,
_GAE_ENV_VARS,
Expand Down Expand Up @@ -867,7 +872,7 @@ class TestSetupLogging(unittest.TestCase):
def _call_fut(self, handler, excludes=None):
from google.cloud.logging.handlers import setup_logging

if excludes:
if excludes is not None:
return setup_logging(handler, excluded_loggers=excludes)
else:
return setup_logging(handler)
Expand All @@ -893,6 +898,24 @@ def test_setup_logging_excludes(self):
self.assertNotIn(handler, excluded_logger.handlers)
self.assertFalse(excluded_logger.propagate)

def test_setup_logging_internal_loggers_no_excludes(self):
handler = _Handler(logging.INFO)
self._call_fut(handler, excludes=())

# Test that excluded logger defaults can be included, but internal
# loggers can't be.
for logger_name in _INTERNAL_LOGGERS:
logger = logging.getLogger(logger_name)
self.assertNotIn(handler, logger.handlers)
self.assertFalse(logger.propagate)

logger = logging.getLogger("logging")
self.assertTrue(logger.propagate)

for logger_name in EXCLUDED_LOGGER_DEFAULTS:
logger = logging.getLogger(logger_name)
self.assertTrue(logger.propagate)

@patch.dict("os.environ", {envar: "1" for envar in _FUNCTION_ENV_VARS})
def test_remove_handlers_gcf(self):
logger = logging.getLogger()
Expand Down Expand Up @@ -939,10 +962,18 @@ def test_keep_handlers_others(self):
def setUp(self):
self._handlers_cache = logging.getLogger().handlers[:]

# reset the logging manager every time so that we're not reusing loggers
# across different test cases.
self._logger_manager = logging.Logger.manager
logging.Logger.manager = logging.Manager(logging.Logger.root)

def tearDown(self):
# cleanup handlers
logging.getLogger().handlers = self._handlers_cache[:]

# restore the old logging manager.
logging.Logger.manager = self._logger_manager


class _Handler(object):
def __init__(self, level):
Expand Down
6 changes: 0 additions & 6 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,6 @@ def test_setup_logging(self):

expected_kwargs = {
"excluded_loggers": (
"google.cloud",
"google.auth",
"google_auth_httplib2",
"google.api_core.bidi",
"werkzeug",
),
Expand Down Expand Up @@ -890,9 +887,6 @@ def test_setup_logging_w_extra_kwargs(self):

expected_kwargs = {
"excluded_loggers": (
"google.cloud",
"google.auth",
"google_auth_httplib2",
"google.api_core.bidi",
"werkzeug",
),
Expand Down