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

Only log controller parameters when configured to. #20

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ In your Gemfile:
# This line is optional if you do not want to suppress app logs in your <environment>.log
config.logstasher.suppress_app_log = false

## Logging params hash

Logstasher can be configured to log the contents of the params hash. When enabled, the contents of the params hash (minus the ActionController internal params)
will be added to the log as a deep hash. This can cause conflicts within the Elasticsearch mappings though, so should be enabled with care. Conflicts will occur
if different actions (or even different applications logging to the same Elasticsearch cluster) use the same params key, but with a different data type (e.g. a
string vs. a hash). This can lead to lost log entries. Enabling this can also significantly increase the size of the Elasticsearch indexes.

To enable this, add the following to your `<environment>.rb`

# Enable logging of controller params
config.logstasher.log_controller_parameters = true

## Adding custom fields to the log

Since some fields are very specific to your application for e.g. *user_name*, so it is left upto you, to add them. Here's how to add those fields to the logs:
Expand Down
10 changes: 7 additions & 3 deletions lib/logstasher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module LogStasher
# Logger for the logstash logs
mattr_accessor :logger, :enabled
mattr_accessor :logger, :enabled, :log_controller_parameters

def self.remove_existing_log_subscriptions
ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
Expand All @@ -33,8 +33,11 @@ def self.unsubscribe(component, subscriber)
def self.add_default_fields_to_payload(payload, request)
payload[:ip] = request.remote_ip
payload[:route] = "#{request.params[:controller]}##{request.params[:action]}"
payload[:parameters] = payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS)
self.custom_fields += [:ip, :route, :parameters]
self.custom_fields += [:ip, :route]
if self.log_controller_parameters
payload[:parameters] = payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS)
self.custom_fields += [:parameters]
end
end

def self.add_custom_fields(&block)
Expand All @@ -52,6 +55,7 @@ def self.setup(app)
self.logger = app.config.logstasher.logger || Logger.new("#{Rails.root}/log/logstash_#{Rails.env}.log")
self.logger.level = app.config.logstasher.log_level || Logger::WARN
self.enabled = true
self.log_controller_parameters = !! app.config.logstasher.log_controller_parameters
end

def self.suppress_app_logs(app)
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/logstasher/log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ def log_output.json
}
before do
LogStasher.logger = logger
LogStasher.log_controller_parameters = true
LogStasher.custom_fields = []
end
after do
LogStasher.log_controller_parameters = false
end

let(:subscriber) {LogStasher::RequestLogSubscriber.new}
let(:event) {
Expand Down
12 changes: 11 additions & 1 deletion spec/lib/logstasher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@
let(:request) { double(:params => params, :remote_ip => '10.0.0.1')}
after do
LogStasher.custom_fields = []
LogStasher.log_controller_parameters = false
end
it 'appends default parameters to payload' do
LogStasher.log_controller_parameters = true
LogStasher.custom_fields = []
LogStasher.add_default_fields_to_payload(payload, request)
payload[:ip].should == '10.0.0.1'
payload[:route].should == 'test#action'
payload[:parameters].should == {'a' => '1', 'b' => 2}
LogStasher.custom_fields.should == [:ip, :route, :parameters]
end

it 'does not include parameters when not configured to' do
LogStasher.custom_fields = []
LogStasher.add_default_fields_to_payload(payload, request)
payload.should_not have_key(:parameters)
LogStasher.custom_fields.should == [:ip, :route]
end
end

describe '.append_custom_params' do
Expand All @@ -59,7 +68,7 @@

describe '.setup' do
let(:logger) { double }
let(:logstasher_config) { double(:logger => logger,:log_level => 'warn') }
let(:logstasher_config) { double(:logger => logger,:log_level => 'warn',:log_controller_parameters => nil) }
let(:config) { double(:logstasher => logstasher_config) }
let(:app) { double(:config => config) }
before do
Expand All @@ -74,6 +83,7 @@
LogStasher.setup(app)
LogStasher.enabled.should be_true
LogStasher.custom_fields.should == []
LogStasher.log_controller_parameters.should == false
end
end

Expand Down