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

Logstasher custom fields in ApplicationController #150

Open
twinster opened this issue Mar 16, 2020 · 0 comments
Open

Logstasher custom fields in ApplicationController #150

twinster opened this issue Mar 16, 2020 · 0 comments

Comments

@twinster
Copy link

I am trying to log additional custom fields using Logstasher gem.

I need to log some response parameters, but logstasher does not support itself logging response params using configuration, therefore I put the code into after_action method in ApplicationController.

ApplicationController

after_action :set_logstasher_params

def set_logstasher_params
  if LogStasher.enabled?
    res_params = JSON.parse(response.body.as_json)
    LogStasher.add_custom_fields do |fields|
      fields[:res_params] = res_params
    end
  end
end

This is logstasher initializer

initializer/logstasher.rb

if LogStasher.enabled?
  LogStasher.add_custom_fields do |fields|
    fields[:request_params] = request.filtered_parameters
    LogStasher::CustomFields.add(:myapi_runtime)
  end

  LogStasher.add_custom_fields_to_request_context do |fields|    
    fields[:request_params] = request.filtered_parameters
  end
end

The problem is next: After starting rails server, first request I send, logs only parameters which are indicated in logstasher.rb except parameters added in ApplicationController.

But After that every request logs everything indicated logstasher.rb as well as ApplicationController method.

Just First request does not log response parameters from ApplicationController method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant