Replace logstasher with lograge

This commit is contained in:
Paul Chavard 2018-08-12 10:31:28 +02:00
parent 8f165c2ced
commit a6a4790a35
6 changed files with 74 additions and 25 deletions

View file

@ -70,6 +70,8 @@ gem 'leaflet-draw-rails'
gem 'chartkick' gem 'chartkick'
gem 'logstasher' gem 'logstasher'
gem 'lograge'
gem 'logstash-event'
gem 'font-awesome-rails' gem 'font-awesome-rails'

View file

@ -457,6 +457,11 @@ GEM
rb-fsevent (~> 0.9, >= 0.9.4) rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7) rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2) ruby_dep (~> 1.2)
lograge (0.10.0)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
logstash-event (1.2.02) logstash-event (1.2.02)
logstasher (1.2.2) logstasher (1.2.2)
activesupport (>= 4.0) activesupport (>= 4.0)
@ -845,6 +850,8 @@ DEPENDENCIES
leaflet-draw-rails leaflet-draw-rails
leaflet-markercluster-rails (~> 0.7.0) leaflet-markercluster-rails (~> 0.7.0)
leaflet-rails leaflet-rails
lograge
logstash-event
logstasher logstasher
mailjet mailjet
maruku maruku

View file

@ -97,15 +97,18 @@ class ApplicationController < ActionController::Base
Raven.user_context(context) Raven.user_context(context)
end end
def session_info_payload def append_info_to_payload(payload)
super
user = logged_user user = logged_user
payload = { payload[:xhr] = !!request.xhr?
payload.merge!({
user_agent: request.user_agent, user_agent: request.user_agent,
current_user_id: user&.id, user_id: user&.id,
current_user_email: user&.email, user_email: user&.email,
current_user_roles: logged_user_roles user_roles: logged_user_roles
}.compact }.compact)
if browser.known? if browser.known?
payload.merge!({ payload.merge!({

View file

@ -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

View file

@ -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

View file

@ -17,7 +17,7 @@ describe ApplicationController, type: :controller do
let(:current_gestionnaire) { nil } let(:current_gestionnaire) { nil }
let(:current_administrateur) { nil } let(:current_administrateur) { nil }
let(:current_administration) { nil } let(:current_administration) { nil }
let(:payload) { @controller.send(:session_info_payload) } let(:payload) { {} }
before do before do
expect(@controller).to receive(:current_user).and_return(current_user) 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) allow(Raven).to receive(:user_context)
@controller.send(:set_raven_context) @controller.send(:set_raven_context)
@controller.send(:append_info_to_payload, payload)
end end
context 'when no one is logged in' do 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' }) .with({ ip_address: '0.0.0.0', roles: 'Guest' })
end 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 end
context 'when a user is logged in' do context 'when a user is logged in' do
@ -47,11 +57,15 @@ describe ApplicationController, type: :controller do
end end
it do it do
[:db_runtime, :view_runtime, :variant, :rendered_format].each do |key|
payload.delete(key)
end
expect(payload).to eq({ expect(payload).to eq({
user_agent: 'Rails Testing', user_agent: 'Rails Testing',
current_user_id: current_user.id, user_id: current_user.id,
current_user_email: current_user.email, user_email: current_user.email,
current_user_roles: 'User' user_roles: 'User',
xhr: false
}) })
end end
end end
@ -68,11 +82,15 @@ describe ApplicationController, type: :controller do
end end
it do it do
[:db_runtime, :view_runtime, :variant, :rendered_format].each do |key|
payload.delete(key)
end
expect(payload).to eq({ expect(payload).to eq({
user_agent: 'Rails Testing', user_agent: 'Rails Testing',
current_user_id: current_user.id, user_id: current_user.id,
current_user_email: current_user.email, user_email: current_user.email,
current_user_roles: 'User, Gestionnaire, Administrateur, Administration' user_roles: 'User, Gestionnaire, Administrateur, Administration',
xhr: false
}) })
end end
end end