Skip to content

Commit

Permalink
Merge pull request #691 from CDLUC3/feature/BR-lograge
Browse files Browse the repository at this point in the history
Add logstash and lograge to the Rails app
  • Loading branch information
briri authored Jan 30, 2025
2 parents 50f617c + e32d23b commit bb05c2c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ gem 'daemons'
# See: https://github.com/igorkasyanchuk/active_storage_validations
gem 'active_storage_validations'

# Lograge is an attempt to bring sanity to Rails' noisy and unusable, unparsable and, in the
# context of running multiple processes and servers, unreadable default logging output.
#
# See: https://github.com/roidrage/lograge
gem 'lograge'

# Logstash is part of the Elastic Stack along with Beats, Elasticsearch and Kibana. Logstash
# is a server-side data processing pipeline that ingests data from a multitude of sources
# simultaneously, transforms it, and then sends it to your favorite "stash."
#
# See: https://github.com/elastic/logstash
gem 'logstash-event'

# ================================= #
# ENVIRONMENT SPECIFIC DEPENDENCIES #
# ================================= #
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ GEM
rb-inotify (~> 0.9, >= 0.9.10)
locale (2.1.4)
logger (1.6.1)
lograge (0.14.0)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
logstash-event (1.2.02)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -563,6 +569,8 @@ GEM
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
request_store (1.7.0)
rack (>= 1.4)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
Expand Down Expand Up @@ -767,6 +775,8 @@ DEPENDENCIES
kaminari
ledermann-rails-settings
listen
lograge
logstash-event
mail
mimemagic
mocha
Expand Down
3 changes: 2 additions & 1 deletion config/initializers/filter_parameter_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
# Use this to limit dissemination of sensitive information.
# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.
Rails.application.config.filter_parameters += [
:password, :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
:password, :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp,
:ssn, :current_password, :password_confirmation, :client_secret
]
34 changes: 34 additions & 0 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Rails.application.configure do
config.lograge.enabled = true

# Use the LogStash format to get JSON instead of the standard Lograge one-liners
config.lograge.formatter = Lograge::Formatters::Logstash.new

# Include controller info in the available log payload
config.lograge.custom_payload do |controller|
{
# host: controller.request.host,
ip: controller.request.ip,
user_id: controller.current_user.try(:id),
}
end

# Skip the Home page because the load balancer pings it every other second and it's noisy
config.lograge.ignore_actions = ['HomeController#index']

# Include the custom info from the event and payload
config.lograge.custom_options = lambda do |event|
params_to_skip = %w[_method action authenticity_token commit controller format id]

{
time: event.time,
params: event.payload[:params].except(*params_to_skip)
}
end

# Continue creating the basic Rails logs
config.lograge.keep_original_rails_log = true

# Define the location of the Lograge format
config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"
end

0 comments on commit bb05c2c

Please sign in to comment.