Skip to content

Commit

Permalink
fix: Allowed for a partial override of loggers that get excluded from…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
gkevinzheng and gcf-owl-bot[bot] committed Jan 9, 2024
1 parent 37aab7f commit 870c940
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
12 changes: 8 additions & 4 deletions google/cloud/logging_v2/handlers/handlers.py
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
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
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

0 comments on commit 870c940

Please sign in to comment.