diff --git a/Gemfile b/Gemfile index 97e2e2e0d..ed7fde5c0 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,8 @@ gem 'leaflet-draw-rails' gem 'chartkick' gem 'logstasher' +gem 'lograge' +gem 'logstash-event' gem 'font-awesome-rails' diff --git a/Gemfile.lock b/Gemfile.lock index cfd127f09..ac66e576a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -457,6 +457,11 @@ GEM rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) + lograge (0.10.0) + actionpack (>= 4) + activesupport (>= 4) + railties (>= 4) + request_store (~> 1.0) logstash-event (1.2.02) logstasher (1.2.2) activesupport (>= 4.0) @@ -845,6 +850,8 @@ DEPENDENCIES leaflet-draw-rails leaflet-markercluster-rails (~> 0.7.0) leaflet-rails + lograge + logstash-event logstasher mailjet maruku diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2ee12840f..0997db1f9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -97,15 +97,18 @@ class ApplicationController < ActionController::Base Raven.user_context(context) end - def session_info_payload + def append_info_to_payload(payload) + super user = logged_user - payload = { + payload[:xhr] = !!request.xhr? + + payload.merge!({ user_agent: request.user_agent, - current_user_id: user&.id, - current_user_email: user&.email, - current_user_roles: logged_user_roles - }.compact + user_id: user&.id, + user_email: user&.email, + user_roles: logged_user_roles + }.compact) if browser.known? payload.merge!({ diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb new file mode 100644 index 000000000..40ba1dcdb --- /dev/null +++ b/config/initializers/lograge.rb @@ -0,0 +1,30 @@ +Rails.application.configure do + config.lograge.formatter = Lograge::Formatters::Logstash.new + config.lograge.base_controller_class = ['ActionController::Base', 'Manager::ApplicationController'] + + # This will allow to override custom options from environement file + # injected by ansible. + if !config.lograge.custom_options + config.lograge.custom_options = lambda do |event| + { + type: 'tps', + user_id: event.payload[:user_id], + user_email: event.payload[:user_email], + user_roles: event.payload[:user_roles], + user_agent: event.payload[:user_agent], + browser: event.payload[:browser], + browser_version: event.payload[:browser_version], + platform: event.payload[:platform] + }.compact + end + + config.lograge.custom_payload do |controller| + { + xhr: !!controller.request.xhr? + } + end + end + + config.lograge.keep_original_rails_log = true + config.lograge.logger = ActiveSupport::Logger.new Rails.root.join('log', "logstash_#{Rails.env}.log") +end diff --git a/config/logstasher.yml b/config/logstasher.yml deleted file mode 100644 index 9ffa58258..000000000 --- a/config/logstasher.yml +++ /dev/null @@ -1,11 +0,0 @@ -backtrace: true -suppress_app_log: false -log_controller_parameters: false -development: - enabled: false -test: - enabled: false -staging: - enabled: false -production: - enabled: true diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index c264994bb..09ae4b18f 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -17,7 +17,7 @@ describe ApplicationController, type: :controller do let(:current_gestionnaire) { nil } let(:current_administrateur) { nil } let(:current_administration) { nil } - let(:payload) { @controller.send(:session_info_payload) } + let(:payload) { {} } before do expect(@controller).to receive(:current_user).and_return(current_user) @@ -27,6 +27,7 @@ describe ApplicationController, type: :controller do allow(Raven).to receive(:user_context) @controller.send(:set_raven_context) + @controller.send(:append_info_to_payload, payload) end context 'when no one is logged in' do @@ -35,7 +36,16 @@ describe ApplicationController, type: :controller do .with({ ip_address: '0.0.0.0', roles: 'Guest' }) end - it { expect(payload).to eq({ user_agent: 'Rails Testing', current_user_roles: 'Guest' }) } + it do + [:db_runtime, :view_runtime, :variant, :rendered_format].each do |key| + payload.delete(key) + end + expect(payload).to eq({ + user_agent: 'Rails Testing', + user_roles: 'Guest', + xhr: false + }) + end end context 'when a user is logged in' do @@ -47,11 +57,15 @@ describe ApplicationController, type: :controller do end it do + [:db_runtime, :view_runtime, :variant, :rendered_format].each do |key| + payload.delete(key) + end expect(payload).to eq({ user_agent: 'Rails Testing', - current_user_id: current_user.id, - current_user_email: current_user.email, - current_user_roles: 'User' + user_id: current_user.id, + user_email: current_user.email, + user_roles: 'User', + xhr: false }) end end @@ -68,11 +82,15 @@ describe ApplicationController, type: :controller do end it do + [:db_runtime, :view_runtime, :variant, :rendered_format].each do |key| + payload.delete(key) + end expect(payload).to eq({ user_agent: 'Rails Testing', - current_user_id: current_user.id, - current_user_email: current_user.email, - current_user_roles: 'User, Gestionnaire, Administrateur, Administration' + user_id: current_user.id, + user_email: current_user.email, + user_roles: 'User, Gestionnaire, Administrateur, Administration', + xhr: false }) end end