Merge pull request #2379 from tchak/lograge

Replace logstasher with lograge
This commit is contained in:
Paul Chavard 2018-08-13 11:38:37 +00:00 committed by GitHub
commit 39a06bec16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 25 deletions

View file

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

View file

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

View file

@ -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!({

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