From 9de3e6e74b1548f9beef72261a1ef6ae3e82dd00 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 13 Aug 2018 16:40:16 +0200 Subject: [PATCH 1/2] Make info extraction method more safe --- app/controllers/application_controller.rb | 2 -- config/initializers/lograge.rb | 2 +- spec/controllers/application_controller_spec.rb | 9 +++------ 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0997db1f9..7ee21f212 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -101,8 +101,6 @@ class ApplicationController < ActionController::Base super user = logged_user - payload[:xhr] = !!request.xhr? - payload.merge!({ user_agent: request.user_agent, user_id: user&.id, diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb index 40ba1dcdb..6a66ef5b9 100644 --- a/config/initializers/lograge.rb +++ b/config/initializers/lograge.rb @@ -20,7 +20,7 @@ Rails.application.configure do config.lograge.custom_payload do |controller| { - xhr: !!controller.request.xhr? + xhr: !!controller&.request&.xhr? } end end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 09ae4b18f..e2d1db65e 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -42,8 +42,7 @@ describe ApplicationController, type: :controller do end expect(payload).to eq({ user_agent: 'Rails Testing', - user_roles: 'Guest', - xhr: false + user_roles: 'Guest' }) end end @@ -64,8 +63,7 @@ describe ApplicationController, type: :controller do user_agent: 'Rails Testing', user_id: current_user.id, user_email: current_user.email, - user_roles: 'User', - xhr: false + user_roles: 'User' }) end end @@ -89,8 +87,7 @@ describe ApplicationController, type: :controller do user_agent: 'Rails Testing', user_id: current_user.id, user_email: current_user.email, - user_roles: 'User, Gestionnaire, Administrateur, Administration', - xhr: false + user_roles: 'User, Gestionnaire, Administrateur, Administration' }) end end From c7b97073ee610220e4cee044d570d7936e196d39 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 13 Aug 2018 16:55:02 +0200 Subject: [PATCH 2/2] Log backtrace on exceptions --- config/initializers/lograge.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb index 6a66ef5b9..f0a191495 100644 --- a/config/initializers/lograge.rb +++ b/config/initializers/lograge.rb @@ -6,6 +6,7 @@ Rails.application.configure do # injected by ansible. if !config.lograge.custom_options config.lograge.custom_options = lambda do |event| + exception_object = event.payload[:exception_object] { type: 'tps', user_id: event.payload[:user_id], @@ -14,7 +15,8 @@ Rails.application.configure do user_agent: event.payload[:user_agent], browser: event.payload[:browser], browser_version: event.payload[:browser_version], - platform: event.payload[:platform] + platform: event.payload[:platform], + backtrace: exception_object ? exception_object.backtrace.join("\n") : nil }.compact end