@@ -337,10 +337,13 @@ def disable_universe_domain_check
337337 # @!attribute [rw] universe_domain
338338 # @return [String] The universe domain issuing these credentials.
339339 #
340+ # @!attribute [rw] logger
341+ # @return [Logger] The logger used to log credential operations such as token refresh.
342+ #
340343 def_delegators :@client ,
341344 :token_credential_uri , :audience ,
342345 :scope , :issuer , :signing_key , :updater_proc , :target_audience ,
343- :universe_domain , :universe_domain=
346+ :universe_domain , :universe_domain= , :logger , :logger=
344347
345348 ##
346349 # Creates a new Credentials instance with the provided auth credentials, and with the default
@@ -349,16 +352,17 @@ def disable_universe_domain_check
349352 # @param [String, Hash, Signet::OAuth2::Client] keyfile
350353 # The keyfile can be provided as one of the following:
351354 #
352- # * The path to a JSON keyfile (as a + String+ )
353- # * The contents of a JSON keyfile (as a + Hash+ )
354- # * A + Signet::OAuth2::Client+ object
355+ # * The path to a JSON keyfile (as a ` String` )
356+ # * The contents of a JSON keyfile (as a ` Hash` )
357+ # * A ` Signet::OAuth2::Client` object
355358 # @param [Hash] options
356359 # The options for configuring the credentials instance. The following is supported:
357360 #
358- # * +:scope+ - the scope for the client
359- # * +"project_id"+ (and optionally +"project"+) - the project identifier for the client
360- # * +:connection_builder+ - the connection builder to use for the client
361- # * +:default_connection+ - the default connection to use for the client
361+ # * `:scope` - the scope for the client
362+ # * `project_id` (and optionally `project`) - the project identifier for the client
363+ # * `:connection_builder` - the connection builder to use for the client
364+ # * `:default_connection` - the default connection to use for the client
365+ # * `:logger` - the logger used to log credential operations such as token refresh.
362366 #
363367 def initialize keyfile , options = { }
364368 verify_keyfile_provided! keyfile
@@ -373,8 +377,8 @@ def initialize keyfile, options = {}
373377 else
374378 update_from_filepath keyfile , options
375379 end
380+ setup_logging logger : options . fetch ( :logger , :default )
376381 @project_id ||= CredentialsLoader . load_gcloud_project_id
377- @client . fetch_access_token! if @client . needs_access_token?
378382 @env_vars = nil
379383 @paths = nil
380384 @scope = nil
@@ -468,7 +472,8 @@ def self.from_io io, options
468472 audience : options [ :audience ] || audience
469473 }
470474 client = Google ::Auth ::DefaultCredentials . make_creds creds_input
471- new client
475+ options = options . select { |k , _v | k == :logger }
476+ new client , options
472477 end
473478
474479 private_class_method :from_env_vars ,
@@ -549,6 +554,23 @@ def update_from_filepath path, options
549554 @quota_project_id ||= json [ "quota_project_id" ]
550555 @client = init_client json , options
551556 end
557+
558+ def setup_logging logger : :default
559+ return unless @client . respond_to? :logger=
560+ logging_env = ENV [ "GOOGLE_SDK_RUBY_LOGGING_GEMS" ] . to_s . downcase
561+ if [ "false" , "none" ] . include? logging_env
562+ logger = nil
563+ elsif @client . logger
564+ logger = @client . logger
565+ elsif logger == :default
566+ logger = nil
567+ if [ "true" , "all" ] . include? ( logging_env ) || logging_env . split ( "," ) . include? ( "googleauth" )
568+ formatter = Google ::Logging ::StructuredFormatter . new if Google ::Cloud ::Env . get . logging_agent_expected?
569+ logger = Logger . new $stderr, progname : "googleauth" , formatter : formatter
570+ end
571+ end
572+ @client . logger = logger
573+ end
552574 end
553575 end
554576end
0 commit comments