Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dist-10392 add logging functionality #55

Merged
merged 23 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
22 changes: 20 additions & 2 deletions lib/muffin_man/finances/v0.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "muffin_man/muffin_logger"
module MuffinMan
module Finances
class V0 < SpApiClient
Expand All @@ -15,7 +16,10 @@ def list_financial_event_groups(max_results_per_page = nil, financial_event_grou
end
@query_params["NextToken"] = next_token unless next_token.nil?
@request_type = "GET"
call_api
res = call_api

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add this in call_api so that any api calls can use this and add enableLogging module wise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

level = res.code == 200 ? :info : :error
log_request_and_response(level, res)
res
end

def list_financial_events_by_group_id(event_group_id, max_results_per_page = nil, posted_after = nil, posted_before = nil, next_token = nil)
Expand All @@ -26,7 +30,21 @@ def list_financial_events_by_group_id(event_group_id, max_results_per_page = nil
@query_params["PostedBefore"] = posted_before unless posted_before.nil?
@query_params["NextToken"] = next_token unless next_token.nil?
@request_type = "GET"
call_api
res = call_api
level = res.code == 200 ? :info : :error
log_request_and_response(level, res)
res
end

def log_request_and_response(level, res)
log_info = "REQUEST\n
canonical_uri:#{canonical_uri}\n\n
query_params:#{query_params}\n\n
RESPONSE\n
response_headers=#{res.headers}\n\n
response_body=#{res.body}\n\n
"
MuffinLogger.logger.send(level, log_info)
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions lib/muffin_man/muffin_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "logger"
module MuffinMan
class MuffinLogger
def self.logger
defined?(Rails) ? Rails.logger : Logger.new($stdout)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making this gem aware of rails, we can add support for the application using the gem to set a logger. Like how Rails supports doing Rails.logger = Logger.new. To do this you can add :logger as an attr_accessor like we have done for :configuration on this line

  class << self
    attr_accessor :configuration, :logger
  end

Then we can use the logger set by the application if one is set with

MuffinMan.logger.info "a message for the logs"

If no logger is set, I wonder if we default to Logger.new("/dev/null") so that nothing actually gets logged but we can still write code assuming MuffinMan.logger is always set.

end

%i[debug info warn error fatal].each do |level|
define_method(level) do |message|
MuffinLogger.logger.send(level, message)
end
end
end
end