Skip to content

Commit

Permalink
add coverage for already configured scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmayr committed Nov 27, 2024
1 parent 601b653 commit 69bbd05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions google/api_core/client_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
] # Additional fields to be Logged.


# TODO(https://github.com/googleapis/python-api-core/issues/763): Add documentation.
def logger_configured(logger):
return (
logger.handlers != [] or logger.level != logging.NOTSET or not logger.propagate
)


# TODO(https://github.com/googleapis/python-api-core/issues/763): Add documentation.
def initialize_logging():
global _LOGGING_INITIALIZED
if _LOGGING_INITIALIZED:
Expand All @@ -30,6 +32,7 @@ def initialize_logging():
_LOGGING_INITIALIZED = True


# TODO(https://github.com/googleapis/python-api-core/issues/763): Add documentation.
def parse_logging_scopes(scopes: Optional[str] = None) -> List[str]:
if not scopes:
return []
Expand All @@ -40,6 +43,7 @@ def parse_logging_scopes(scopes: Optional[str] = None) -> List[str]:
return namespaces


# TODO(https://github.com/googleapis/python-api-core/issues/763): Add documentation.
def configure_defaults(logger):
if not logger_configured(logger):
console_handler = logging.StreamHandler()
Expand All @@ -50,6 +54,7 @@ def configure_defaults(logger):
logger.addHandler(console_handler)


# TODO(https://github.com/googleapis/python-api-core/issues/763): Add documentation.
def setup_logging(scopes=""):

# only returns valid logger scopes (namespaces)
Expand All @@ -69,6 +74,7 @@ def setup_logging(scopes=""):
base_logger.propagate = False


# TODO(https://github.com/googleapis/python-api-core/issues/763): Add documentation.
class StructuredLogFormatter(logging.Formatter):
# TODO(https://github.com/googleapis/python-api-core/issues/761): ensure that additional fields such as
# function name, file name, and line no. appear in a log output.
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_client_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def test_setup_logging_w_base_scope():
reset_logger("foo")


def test_setup_logging_w_configured_scope():
with mock.patch("google.api_core.client_logging._BASE_LOGGER_NAME", "foo"):
base_logger = logging.getLogger("foo")
base_logger.propagate = False
setup_logging("foo")
assert base_logger.handlers == []
assert not base_logger.propagate
assert base_logger.level == logging.NOTSET

reset_logger("foo")


def test_setup_logging_w_module_scope():
with mock.patch("google.api_core.client_logging._BASE_LOGGER_NAME", "foo"):
setup_logging("foo.bar")
Expand Down

0 comments on commit 69bbd05

Please sign in to comment.