Skip to content

Commit 870c940

Browse files
fix: Allowed for a partial override of loggers that get excluded from setup_client (#831)
* fix: Allowed for a partial override of loggers that get excluded from setup_client * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 37aab7f commit 870c940

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

google/cloud/logging_v2/handlers/handlers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424

2525
DEFAULT_LOGGER_NAME = "python"
2626

27-
"""Exclude internal logs from propagating through handlers"""
27+
"""Defaults for filtering out noisy loggers"""
2828
EXCLUDED_LOGGER_DEFAULTS = (
29+
"google.api_core.bidi",
30+
"werkzeug",
31+
)
32+
33+
"""Exclude internal logs from propagating through handlers"""
34+
_INTERNAL_LOGGERS = (
2935
"google.cloud",
3036
"google.auth",
3137
"google_auth_httplib2",
32-
"google.api_core.bidi",
33-
"werkzeug",
3438
)
3539

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

297301
# remove built-in handlers on App Engine or Cloud Functions environments

tests/unit/handlers/test_handlers.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import mock
1919
import json
2020

21+
from google.cloud.logging_v2.handlers.handlers import (
22+
_INTERNAL_LOGGERS,
23+
EXCLUDED_LOGGER_DEFAULTS,
24+
)
25+
2126
from google.cloud.logging_v2.handlers._monitored_resources import (
2227
_FUNCTION_ENV_VARS,
2328
_GAE_ENV_VARS,
@@ -867,7 +872,7 @@ class TestSetupLogging(unittest.TestCase):
867872
def _call_fut(self, handler, excludes=None):
868873
from google.cloud.logging.handlers import setup_logging
869874

870-
if excludes:
875+
if excludes is not None:
871876
return setup_logging(handler, excluded_loggers=excludes)
872877
else:
873878
return setup_logging(handler)
@@ -893,6 +898,24 @@ def test_setup_logging_excludes(self):
893898
self.assertNotIn(handler, excluded_logger.handlers)
894899
self.assertFalse(excluded_logger.propagate)
895900

901+
def test_setup_logging_internal_loggers_no_excludes(self):
902+
handler = _Handler(logging.INFO)
903+
self._call_fut(handler, excludes=())
904+
905+
# Test that excluded logger defaults can be included, but internal
906+
# loggers can't be.
907+
for logger_name in _INTERNAL_LOGGERS:
908+
logger = logging.getLogger(logger_name)
909+
self.assertNotIn(handler, logger.handlers)
910+
self.assertFalse(logger.propagate)
911+
912+
logger = logging.getLogger("logging")
913+
self.assertTrue(logger.propagate)
914+
915+
for logger_name in EXCLUDED_LOGGER_DEFAULTS:
916+
logger = logging.getLogger(logger_name)
917+
self.assertTrue(logger.propagate)
918+
896919
@patch.dict("os.environ", {envar: "1" for envar in _FUNCTION_ENV_VARS})
897920
def test_remove_handlers_gcf(self):
898921
logger = logging.getLogger()
@@ -939,10 +962,18 @@ def test_keep_handlers_others(self):
939962
def setUp(self):
940963
self._handlers_cache = logging.getLogger().handlers[:]
941964

965+
# reset the logging manager every time so that we're not reusing loggers
966+
# across different test cases.
967+
self._logger_manager = logging.Logger.manager
968+
logging.Logger.manager = logging.Manager(logging.Logger.root)
969+
942970
def tearDown(self):
943971
# cleanup handlers
944972
logging.getLogger().handlers = self._handlers_cache[:]
945973

974+
# restore the old logging manager.
975+
logging.Logger.manager = self._logger_manager
976+
946977

947978
class _Handler(object):
948979
def __init__(self, level):

tests/unit/test_client.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,6 @@ def test_setup_logging(self):
847847

848848
expected_kwargs = {
849849
"excluded_loggers": (
850-
"google.cloud",
851-
"google.auth",
852-
"google_auth_httplib2",
853850
"google.api_core.bidi",
854851
"werkzeug",
855852
),
@@ -890,9 +887,6 @@ def test_setup_logging_w_extra_kwargs(self):
890887

891888
expected_kwargs = {
892889
"excluded_loggers": (
893-
"google.cloud",
894-
"google.auth",
895-
"google_auth_httplib2",
896890
"google.api_core.bidi",
897891
"werkzeug",
898892
),

0 commit comments

Comments
 (0)