# frozen_string_literal: true require 'ecs_logging/logger' require 'rails' module Loggable # 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) Rails.logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase || 'INFO') end # 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': user_id, 'http.code': response_code }, status: response_code } end config.lograge.custom_options = lambda do |event| exceptions = %w[controller action format id] { 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 Delayed::Worker.logger = ::EcsLogging::Logger.new($stdout) Delayed::Worker.logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase || 'INFO') else Delayed::Worker.logger = Logger.new($stdout) Delayed::Worker.logger.level = Logger::DEBUG Delayed::Worker.logger.datetime_format = '%Y-%m-%d %H:%M:%S' end end end end end end