lib/logstasher.rb in logstasher-0.5.3 vs lib/logstasher.rb in logstasher-0.6.0

- old
+ new

@@ -6,22 +6,25 @@ require 'active_support/ordered_options' module LogStasher extend self STORE_KEY = :logstasher_data + REQUEST_CONTEXT_KEY = :logstasher_request_context - attr_accessor :logger, :enabled, :log_controller_parameters, :source + attr_accessor :logger, :logger_path, :enabled, :log_controller_parameters, :source # Setting the default to 'unknown' to define the default behaviour @source = 'unknown' def remove_existing_log_subscriptions ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber| case subscriber when ActionView::LogSubscriber unsubscribe(:action_view, subscriber) when ActionController::LogSubscriber unsubscribe(:action_controller, subscriber) + when ActionMailer::LogSubscriber + unsubscribe(:action_mailer, subscriber) end end end def unsubscribe(component, subscriber) @@ -36,11 +39,12 @@ end def add_default_fields_to_payload(payload, request) payload[:ip] = request.remote_ip payload[:route] = "#{request.params[:controller]}##{request.params[:action]}" - self.custom_fields += [:ip, :route] + payload[:request_id] = request.env['action_dispatch.request_id'] + self.custom_fields += [:ip, :route, :request_id] if self.log_controller_parameters payload[:parameters] = payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS) self.custom_fields += [:parameters] end end @@ -52,18 +56,37 @@ end ActionController::Metal.send(:define_method, :logtasher_add_custom_fields_to_payload, &wrapped_block) ActionController::Base.send(:define_method, :logtasher_add_custom_fields_to_payload, &wrapped_block) end + def add_custom_fields_to_request_context(&block) + wrapped_block = Proc.new do |fields| + instance_exec(fields, &block) + LogStasher.custom_fields.concat(fields.keys) + end + ActionController::Metal.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block) + ActionController::Base.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block) + end + + def add_default_fields_to_request_context(request) + request_context[:request_id] = request.env['action_dispatch.request_id'] + end + + def clear_request_context + request_context.clear + end + def setup(app) app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache # Path instrumentation class to insert our hook require 'logstasher/rails_ext/action_controller/metal/instrumentation' require 'logstash-event' self.suppress_app_logs(app) LogStasher::RequestLogSubscriber.attach_to :action_controller - self.logger = app.config.logstasher.logger || new_logger("#{Rails.root}/log/logstash_#{Rails.env}.log") + LogStasher::MailerLogSubscriber.attach_to :action_mailer + self.logger_path = app.config.logstasher.logger_path || "#{Rails.root}/log/logstash_#{Rails.env}.log" + self.logger = app.config.logstasher.logger || new_logger(self.logger_path) self.logger.level = app.config.logstasher.log_level || Logger::WARN self.source = app.config.logstasher.source unless app.config.logstasher.source.nil? self.enabled = true self.log_controller_parameters = !! app.config.logstasher.log_controller_parameters end @@ -101,10 +124,14 @@ RequestStore.store[STORE_KEY] = Hash.new { |hash, key| hash[key] = {} } end RequestStore.store[STORE_KEY] end + def request_context + RequestStore.store[REQUEST_CONTEXT_KEY] ||= {} + end + def watch(event, opts = {}, &block) event_group = opts[:event_group] || event ActiveSupport::Notifications.subscribe(event) do |*args| # Calling the processing block with the Notification args and the store block.call(*args, store[event_group]) @@ -115,9 +142,13 @@ eval <<-EOM, nil, __FILE__, __LINE__ + 1 def #{severity}(msg) self.log(:#{severity}, msg) end EOM + end + + def enabled? + self.enabled end private def new_logger(path)