44import os
55
66_LOGGING_INITIALIZED = False
7-
8- # TODO(<add-link> ): Update Request / Response messages.
7+ _BASE_LOGGER_NAME = "google"
8+ # TODO(https://github.com/googleapis/python-api-core/issues/760 ): Update Request / Response messages.
99REQUEST_MESSAGE = "Sending request ..."
1010RESPONSE_MESSAGE = "Receiving response ..."
1111
12- # TODO(<add-link> ): Update this list to support additional logging fields
12+ # TODO(https://github.com/googleapis/python-api-core/issues/761 ): Update this list to support additional logging fields
1313_recognized_logging_fields = ["httpRequest" , "rpcName" , "serviceName" ] # Additional fields to be Logged.
1414
1515def logger_configured (logger ):
16- return logger .hasHandlers () or logger .level != logging .NOTSET or logger .propagate == False
16+ return logger .handlers != [] or logger .level != logging .NOTSET or logger .propagate == False
1717
1818def initialize_logging ():
1919 global _LOGGING_INITIALIZED
@@ -26,8 +26,9 @@ def initialize_logging():
2626def parse_logging_scopes (scopes ):
2727 if not scopes :
2828 return []
29- # TODO(<add-link>): check if the namespace is a valid namespace.
30- # TODO(<add-link>): parse a list of namespaces. Current flow expects a single string for now.
29+ # TODO(https://github.com/googleapis/python-api-core/issues/759): check if the namespace is a valid namespace.
30+ # TODO(b/380481951): Support logging multiple scopes.
31+ # TODO(b/380483756): Raise or log a warning for an invalid scope.
3132 namespaces = [scopes ]
3233 return namespaces
3334
@@ -40,23 +41,25 @@ def configure_defaults(logger):
4041 console_handler .setFormatter (formatter )
4142 logger .addHandler (console_handler )
4243
43- def setup_logging (scopes ):
44- # disable log propagation at base logger level to the root logger only if a base logger is not already configured via code changes.
45- base_logger = logging .getLogger ("google" )
46- if not logger_configured (base_logger ):
47- base_logger .propagate = False
44+ def setup_logging (scopes = []):
4845
4946 # only returns valid logger scopes (namespaces)
5047 # this list has at most one element.
51- loggers = parse_logging_scopes (scopes )
48+ logger_names = parse_logging_scopes (scopes )
5249
53- for namespace in loggers :
50+ for namespace in logger_names :
5451 # This will either create a module level logger or get the reference of the base logger instantiated above.
5552 logger = logging .getLogger (namespace )
5653
5754 # Configure default settings.
5855 configure_defaults (logger )
5956
57+ # disable log propagation at base logger level to the root logger only if a base logger is not already configured via code changes.
58+ # Maybe we do this at the end?
59+ base_logger = logging .getLogger (_BASE_LOGGER_NAME )
60+ if not logger_configured (base_logger ):
61+ base_logger .propagate = False
62+
6063class StructuredLogFormatter (logging .Formatter ):
6164 def format (self , record ):
6265 log_obj = {
0 commit comments