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

Support custom_payload for Rails 5 API's ActionController::API #227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ Rails.application.configure do
end
```

If you're using Rails 5's API-only mode and inherit from `ActionController::API`:

```ruby
# config/initializers/lograge.rb
Rails.application.configure do
config.lograge.base_controller_class = ActionController::API
end
```

You can also add a hook for own custom data

```ruby
Expand Down
5 changes: 3 additions & 2 deletions lib/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ def attach_to_action_controller

def setup_custom_payload
return unless lograge_config.custom_payload_method.respond_to?(:call)
append_payload_method = ActionController::Base.instance_method(:append_info_to_payload)
base_controller_class = lograge_config.base_controller_class || ActionController::Base
append_payload_method = base_controller_class.instance_method(:append_info_to_payload)
custom_payload_method = lograge_config.custom_payload_method

ActionController::Base.send(:define_method, :append_info_to_payload) do |payload|
base_controller_class.send(:define_method, :append_info_to_payload) do |payload|
append_payload_method.bind(self).call(payload)
payload[:custom_payload] = custom_payload_method.call(self)
end
Expand Down
22 changes: 20 additions & 2 deletions spec/lograge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
end

describe 'handling custom_payload option' do
let(:controller_class) { 'ActionController::Base' }
let(:app_config) do
config_obj = ActiveSupport::OrderedOptions.new.tap do |config|
config.action_dispatch = double(rack_cache: false)
Expand All @@ -119,12 +120,29 @@ def current_user_id
subject { payload.dup }

before do
stub_const('ActionController::Base', controller)
stub_const(controller_class, controller)
Lograge.setup(app_config)
ActionController::Base.new.append_info_to_payload(subject)
controller_class.constantize.new.append_info_to_payload(subject)
end

it { should eq(payload.merge(appended: true, custom_payload: { user_id: '24601' })) }

context 'when base_controller_class option is set' do
let(:controller_class) { 'ActionController::API' }
let(:app_config) do
config_obj = ActiveSupport::OrderedOptions.new.tap do |config|
config.action_dispatch = double(rack_cache: false)
config.lograge = Lograge::OrderedOptions.new
config.lograge.base_controller_class = controller_class.constantize
config.lograge.custom_payload do |c|
{ user_id: c.current_user_id }
end
end
double(config: config_obj)
end

it { should eq(payload.merge(appended: true, custom_payload: { user_id: '24601' })) }
end
end

describe 'deprecated log_format interpreter' do
Expand Down