Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add coverage for already configured scope
  • Loading branch information
ohmayr committed Nov 27, 2024
commit 69bbd059f4f7fd0730daa9d45fabeaf37696dd15
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.
Comment thread
ohmayr marked this conversation as resolved.


# TODO(https://github.com/googleapis/python-api-core/issues/763): Add documentation.
def logger_configured(logger):
Comment thread
ohmayr marked this conversation as resolved.
Outdated
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():
Comment thread
ohmayr marked this conversation as resolved.
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=""):
Comment thread
ohmayr marked this conversation as resolved.
Outdated

# 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