diff --git a/Gemfile b/Gemfile index 56a0296986..a05c88a70b 100644 --- a/Gemfile +++ b/Gemfile @@ -273,6 +273,12 @@ 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' + # ================================= # # ENVIRONMENT SPECIFIC DEPENDENCIES # # ================================= # diff --git a/Gemfile.lock b/Gemfile.lock index 79b430432e..2470f84a9d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -381,6 +381,11 @@ 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) loofah (2.23.1) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -563,6 +568,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) @@ -767,6 +774,7 @@ DEPENDENCIES kaminari ledermann-rails-settings listen + lograge mail mimemagic mocha diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb new file mode 100644 index 0000000000..2153d08cf0 --- /dev/null +++ b/config/initializers/lograge.rb @@ -0,0 +1,37 @@ +Rails.application.configure do + config.lograge.enabled = true + + # Use the LogStash format + 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, + user_id: controller.current_user.try(:id) + params: controller.params + } + end + + # Include the custom info from the event and payload + config.lograge.custom_options = lambda do |event| + param_exceptions = %w(controller action format id) + + { + # Timestamp + time: event.time, + # Controller params + params: event.payload[:params].except(*param_exceptions), + # The current user + user: event.payload[:user_id], + # Caller + host: event.payload[:host] + } + 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 \ No newline at end of file