Skip to content

Commit bb05c2c

Browse files
authored
Merge pull request #691 from CDLUC3/feature/BR-lograge
Add logstash and lograge to the Rails app
2 parents 50f617c + e32d23b commit bb05c2c

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

Gemfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,19 @@ gem 'daemons'
273273
# See: https://github.com/igorkasyanchuk/active_storage_validations
274274
gem 'active_storage_validations'
275275

276+
# Lograge is an attempt to bring sanity to Rails' noisy and unusable, unparsable and, in the
277+
# context of running multiple processes and servers, unreadable default logging output.
278+
#
279+
# See: https://github.com/roidrage/lograge
280+
gem 'lograge'
281+
282+
# Logstash is part of the Elastic Stack along with Beats, Elasticsearch and Kibana. Logstash
283+
# is a server-side data processing pipeline that ingests data from a multitude of sources
284+
# simultaneously, transforms it, and then sends it to your favorite "stash."
285+
#
286+
# See: https://github.com/elastic/logstash
287+
gem 'logstash-event'
288+
276289
# ================================= #
277290
# ENVIRONMENT SPECIFIC DEPENDENCIES #
278291
# ================================= #

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ GEM
381381
rb-inotify (~> 0.9, >= 0.9.10)
382382
locale (2.1.4)
383383
logger (1.6.1)
384+
lograge (0.14.0)
385+
actionpack (>= 4)
386+
activesupport (>= 4)
387+
railties (>= 4)
388+
request_store (~> 1.0)
389+
logstash-event (1.2.02)
384390
loofah (2.23.1)
385391
crass (~> 1.0.2)
386392
nokogiri (>= 1.12.0)
@@ -563,6 +569,8 @@ GEM
563569
regexp_parser (2.9.2)
564570
reline (0.5.10)
565571
io-console (~> 0.5)
572+
request_store (1.7.0)
573+
rack (>= 1.4)
566574
responders (3.1.1)
567575
actionpack (>= 5.2)
568576
railties (>= 5.2)
@@ -767,6 +775,8 @@ DEPENDENCIES
767775
kaminari
768776
ledermann-rails-settings
769777
listen
778+
lograge
779+
logstash-event
770780
mail
771781
mimemagic
772782
mocha

config/initializers/filter_parameter_logging.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
# Use this to limit dissemination of sensitive information.
55
# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.
66
Rails.application.config.filter_parameters += [
7-
:password, :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
7+
:password, :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp,
8+
:ssn, :current_password, :password_confirmation, :client_secret
89
]

config/initializers/lograge.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Rails.application.configure do
2+
config.lograge.enabled = true
3+
4+
# Use the LogStash format to get JSON instead of the standard Lograge one-liners
5+
config.lograge.formatter = Lograge::Formatters::Logstash.new
6+
7+
# Include controller info in the available log payload
8+
config.lograge.custom_payload do |controller|
9+
{
10+
# host: controller.request.host,
11+
ip: controller.request.ip,
12+
user_id: controller.current_user.try(:id),
13+
}
14+
end
15+
16+
# Skip the Home page because the load balancer pings it every other second and it's noisy
17+
config.lograge.ignore_actions = ['HomeController#index']
18+
19+
# Include the custom info from the event and payload
20+
config.lograge.custom_options = lambda do |event|
21+
params_to_skip = %w[_method action authenticity_token commit controller format id]
22+
23+
{
24+
time: event.time,
25+
params: event.payload[:params].except(*params_to_skip)
26+
}
27+
end
28+
29+
# Continue creating the basic Rails logs
30+
config.lograge.keep_original_rails_log = true
31+
32+
# Define the location of the Lograge format
33+
config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"
34+
end

0 commit comments

Comments
 (0)