Skip to content

Commit

Permalink
address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmayr committed Nov 25, 2024
1 parent 7b1605c commit 48b9ed9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
16 changes: 8 additions & 8 deletions google/api_core/client_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import os

LOGGING_INITIALIZED = False
_LOGGING_INITIALIZED = False

# TODO(<add-link>): Update Request / Response messages.
REQUEST_MESSAGE = "Sending request ..."
Expand All @@ -13,15 +13,15 @@
_recognized_logging_fields = ["httpRequest", "rpcName", "serviceName"] # Additional fields to be Logged.

def logger_configured(logger):
return logger.hasHandlers() or logger.level != logging.NOTSET
return logger.hasHandlers() or logger.level != logging.NOTSET or logger.propagate == False

def initialize_logging():
global LOGGING_INITIALIZED
if LOGGING_INITIALIZED:
global _LOGGING_INITIALIZED
if _LOGGING_INITIALIZED:
return
scopes = os.getenv("GOOGLE_SDK_PYTHON_LOGGING_SCOPE")
setup_logging(scopes)
LOGGING_INITIALIZED = True
_LOGGING_INITIALIZED = True

def parse_logging_scopes(scopes):
if not scopes:
Expand All @@ -31,7 +31,7 @@ def parse_logging_scopes(scopes):
namespaces = [scopes]
return namespaces

def default_settings(logger):
def configure_defaults(logger):
if not logger_configured(logger):
console_handler = logging.StreamHandler()
logger.setLevel("DEBUG")
Expand All @@ -54,8 +54,8 @@ def setup_logging(scopes):
# This will either create a module level logger or get the reference of the base logger instantiated above.
logger = logging.getLogger(namespace)

# Set default settings.
default_settings(logger)
# Configure default settings.
configure_defaults(logger)

class StructuredLogFormatter(logging.Formatter):
def format(self, record):
Expand Down
23 changes: 13 additions & 10 deletions tests/unit/test_client_logging.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import logging
import pytest

from google.api_core.client_logging import BaseLogger
# Test expected behaviour for warnings, propagation, handler + formatter.

def test_setup_logging_w_no_scopes():
# TODO(in-progress):
pass

def test_base_logger(caplog):
def test_setup_logging_w_base_scope():
# TODO(in-progress):
pass

logger = BaseLogger().get_logger()
def test_setup_logging_w_module_scope():
# TODO(in-progress):
pass

with caplog.at_level(logging.INFO, logger="google"):
logger.info("This is a test message.")

assert "This is a test message." in caplog.text
assert caplog.records[0].name == "google"
assert caplog.records[0].levelname == "INFO"
assert caplog.records[0].message == "This is a test message."
def test_setup_logging_w_incorrect_scope():
# TODO(in-progress):
pass

0 comments on commit 48b9ed9

Please sign in to comment.