Refactor logging across controllers and jobs to use a centralized log method; improve consistency and readability of log messages

This commit is contained in:
2026-02-27 22:32:07 -05:00
parent f32b48296d
commit 9c0f153518
10 changed files with 107 additions and 31 deletions

View File

@@ -13,12 +13,13 @@ module QuickbooksOauth
#== Instance Methods
# This method will attempt to execute the block and if it encounters an OAuth2::Error or Quickbooks::AuthorizationFailure it will attempt to refresh the token and retry the block. It will try this up to 3 times before giving up and raising an exception.
def perform_authenticated_request(&block)
attempts = 0
begin
yield oauth_access_token
rescue OAuth2::Error, Quickbooks::AuthorizationFailure => ex
Rails.logger.error("QuickbooksOauth.perform: #{ex.message}")
log "perform_authenticated_request: #{ex.message}"
# to prevent an infinite loop here keep a counter and bail out after N times...
attempts += 1
@@ -32,8 +33,9 @@ module QuickbooksOauth
end
end
# This method will attempt to refresh the access token and update the record with the new access token, refresh token and their respective expiration times. If the refresh token expires in more than 0 seconds then we will set the refresh token expiration time to that value, otherwise we will set it to 100 days from now.
def refresh_token!
Rails.logger.info("QuickbooksOauth.refresh_token!")
log "refresh_token!"
t = oauth_access_token
refreshed = t.refresh!
@@ -43,7 +45,7 @@ module QuickbooksOauth
oauth2_refresh_token_expires_at = 100.days.from_now
end
Rails.logger.info("QuickbooksOauth.refresh_token!: #{oauth2_refresh_token_expires_at}")
log "refresh_token!: #{oauth2_refresh_token_expires_at}"
update!(
oauth2_access_token: refreshed.token,
@@ -53,20 +55,24 @@ module QuickbooksOauth
)
end
# This method will return an instance of the OAuth2::Client class that is configured with the consumer key, consumer secret and the appropriate URLs for the Intuit OAuth2 service. It will also set the sandbox mode based on the plugin settings.
def oauth_client
self.class.construct_oauth2_client
end
# This method will return an instance of the OAuth2::AccessToken class that is configured with the current access token, refresh token and the OAuth2 client. This access token can be used to make authenticated requests to the Intuit API.
def oauth_access_token
OAuth2::AccessToken.new(oauth_client, oauth2_access_token, refresh_token: oauth2_refresh_token)
end
# This method is an alias for the oauth_access_token method and is used to provide a more intuitive name for the access token when making authenticated requests.
def consumer
oauth_access_token
end
module ClassMethods
# This method will construct and return an instance of the OAuth2::Client class that is configured with the consumer key, consumer secret and the appropriate URLs for the Intuit OAuth2 service. It will also set the sandbox mode based on the plugin settings. This method is used by the instance method oauth_client to create a new OAuth2 client for each instance of the model that includes this concern.
def construct_oauth2_client
oauth_consumer_key = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey']
@@ -74,7 +80,7 @@ module QuickbooksOauth
# Are we are playing in the sandbox?
Quickbooks.sandbox_mode = Setting.plugin_redmine_qbo[:sandbox] ? true : false
logger.info "Sandbox mode: #{Quickbooks.sandbox_mode}"
log "Sandbox mode: #{Quickbooks.sandbox_mode}"
options = {
site: "https://appcenter.intuit.com/connect/oauth2",
@@ -85,4 +91,11 @@ module QuickbooksOauth
end
end
private
def log(msg)
Rails.logger.info "[QuickbooksOauth] #{msg}"
end
end

View File

@@ -27,7 +27,7 @@ class Estimate < ActiveRecord::Base
# sync only one estimate
def self.sync_by_id(id)
logger.info "Syncing estimate #{id}"
log "Syncing estimate #{id}"
qbo = Qbo.first
qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: qbo.realm_id, access_token: access_token)
@@ -37,7 +37,7 @@ class Estimate < ActiveRecord::Base
# sync only one estimate
def self.sync_by_doc_number(number)
logger.info "Syncing estimate by doc number #{number}"
log "Syncing estimate by doc number #{number}"
qbo = Qbo.first
qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: qbo.realm_id, access_token: access_token)
@@ -63,7 +63,7 @@ class Estimate < ActiveRecord::Base
# process an estimate into the database
def self.process_estimate(qbo_estimate)
logger.info "Processing estimate #{qbo_estimate.id}"
log "Processing estimate #{qbo_estimate.id}"
estimate = find_or_create_by(id: qbo_estimate.id)
estimate.doc_number = qbo_estimate.doc_number
estimate.customer_id = qbo_estimate.customer_ref.value
@@ -115,5 +115,9 @@ class Estimate < ActiveRecord::Base
@details = Quickbooks::Model::Estimate.new
end
end
def log(msg)
Rails.logger.info "[Estimate] #{msg}"
end
end

View File

@@ -16,7 +16,7 @@ class Qbo < ActiveRecord::Base
# Updates last sync time stamp
def self.update_time_stamp
date = DateTime.now
logger.info "Updating QBO timestamp to #{date}"
log "Updating QBO timestamp to #{date}"
qbo = Qbo.first
qbo.last_sync = date
qbo.save
@@ -25,4 +25,11 @@ class Qbo < ActiveRecord::Base
def self.last_sync
format_time(Qbo.first.last_sync)
end
private
def self.log(msg)
logger.info "[QBO] #{msg}"
end
end