lib/loggable/railtie.rb in researchable_loggable-1.0.2 vs lib/loggable/railtie.rb in researchable_loggable-1.1.0

- old
+ new

@@ -7,13 +7,15 @@ # Railtie to configure logging before rails starts class Railtie < Rails::Railtie # We add a new loggable namespace to the config object, to keep all the configuration related to this gem organized config.loggable = ActiveSupport::OrderedOptions.new config.loggable.production_like = false + config.loggable.current_user_method = :current_user # Initializer runs before initialize_logger (found in Bootstrap) so from the very beginning we are logging using # the ecs format, even during the initialization process + # rubocop:disable Metrics/BlockLength initializer :loggable_web, before: :initialize_logger do Rails.application.configure do production_like = Rails.env.production? || config.loggable.production_like if production_like Rails.logger = ::EcsLogging::Logger.new($stdout) @@ -23,14 +25,17 @@ # lograge configuration config.lograge.enabled = true config.lograge.base_controller_class = 'ApplicationController' config.lograge.custom_payload do |controller| response_code = controller.response.code + if controller.respond_to?(config.loggable.current_user_method) + user_id = controller.send(config.loggable.current_user_method).try(:id) + end { ecs: { 'source.ip': controller.request.ip, - 'user.id': controller.current_user.try(:id), + 'user.id': user_id, 'http.code': response_code }, status: response_code } end @@ -39,9 +44,10 @@ { params: event.payload[:params].except(*exceptions) } end config.lograge.formatter = Loggable::Lograge::Formatter.new if production_like end end + # rubocop:enable Metrics/BlockLength initializer :loggable_worker, before: :initializer_logger do Rails.application.configure do if defined?(Delayed::Worker) if Rails.env.production? || config.loggable.production_like