lib/logstasher.rb in logstasher-0.7.1 vs lib/logstasher.rb in logstasher-0.8.0
- old
+ new
@@ -1,25 +1,30 @@
require 'logstasher/version'
-require 'logstasher/log_subscriber'
+require 'logstasher/active_support/log_subscriber'
+require 'logstasher/active_support/mailer_log_subscriber'
+require 'logstasher/active_record/log_subscriber'
+require 'logstasher/action_view/log_subscriber'
+require 'logstasher/rails_ext/action_controller/base'
require 'request_store'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'
require 'active_support/ordered_options'
module LogStasher
extend self
STORE_KEY = :logstasher_data
REQUEST_CONTEXT_KEY = :logstasher_request_context
- attr_accessor :logger, :logger_path, :enabled, :log_controller_parameters, :source, :backtrace
+ attr_accessor :logger, :logger_path, :enabled, :log_controller_parameters, :source, :backtrace,
+ :delayed_jobs_support, :controller_monkey_patch
# Setting the default to 'unknown' to define the default behaviour
@source = 'unknown'
# By default log the backtrace of exceptions
@backtrace = true
def remove_existing_log_subscriptions
- ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
+ ::ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
case subscriber.class.name
when 'ActionView::LogSubscriber'
unsubscribe(:action_view, subscriber)
when 'ActionController::LogSubscriber'
unsubscribe(:action_controller, subscriber)
@@ -30,13 +35,13 @@
end
def unsubscribe(component, subscriber)
events = subscriber.public_methods(false).reject{ |method| method.to_s == 'call' }
events.each do |event|
- ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
+ ::ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
if listener.instance_variable_get('@delegate') == subscriber
- ActiveSupport::Notifications.unsubscribe listener
+ ::ActiveSupport::Notifications.unsubscribe listener
end
end
end
end
@@ -44,68 +49,100 @@
payload[:ip] = request.remote_ip
payload[:route] = "#{request.params[:controller]}##{request.params[:action]}"
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)
+ payload[:parameters] = payload[:params].except(*::ActionController::LogSubscriber::INTERNAL_PARAMS)
self.custom_fields += [:parameters]
end
end
def add_custom_fields(&block)
wrapped_block = Proc.new do |fields|
LogStasher.custom_fields.concat(LogStasher.store.keys)
instance_exec(fields, &block)
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)
+ ::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)
+ ::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'
+ def setup_before(config)
require 'logstash-event'
- self.suppress_app_logs(app)
- LogStasher::RequestLogSubscriber.attach_to :action_controller
- 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
- self.backtrace = !! app.config.logstasher.backtrace unless app.config.logstasher.backtrace.nil?
+ self.enabled = config.enabled
+ LogStasher::ActiveSupport::LogSubscriber.attach_to :action_controller
+ LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer
+ LogStasher::ActiveRecord::LogSubscriber.attach_to :active_record
+ LogStasher::ActionView::LogSubscriber.attach_to :action_view
end
- def suppress_app_logs(app)
- if configured_to_suppress_app_logs?(app)
+ def setup(config)
+ # Path instrumentation class to insert our hook
+ if (! config.controller_monkey_patch && config.controller_monkey_patch != false) || config.controller_monkey_patch == true
+ require 'logstasher/rails_ext/action_controller/metal/instrumentation'
+ end
+ self.delayed_plugin(config)
+ self.suppress_app_logs(config)
+ self.logger_path = config.logger_path || "#{Rails.root}/log/logstash_#{Rails.env}.log"
+ self.logger = config.logger || new_logger(self.logger_path)
+ self.logger.level = config.log_level || Logger::WARN
+ self.source = config.source unless config.source.nil?
+ self.log_controller_parameters = !! config.log_controller_parameters
+ self.backtrace = !! config.backtrace unless config.backtrace.nil?
+ self.set_data_for_rake
+ self.set_data_for_console
+ end
+
+ def set_data_for_rake
+ self.request_context['request_id'] = ::Rake.application.top_level_tasks if self.called_as_rake?
+ end
+
+ def set_data_for_console
+ self.request_context['request_id'] = "#{Process.pid}" if self.called_as_console?
+ end
+
+ def called_as_rake?
+ File.basename($0) == 'rake'
+ end
+
+ def called_as_console?
+ defined?(Rails::Console) && true || false
+ end
+
+ def delayed_plugin(config)
+ if config.delayed_jobs_support || false
+ require 'logstasher/delayed/plugin'
+ ::Delayed::Worker.plugins << ::LogStasher::Delayed::Plugin
+ end
+ end
+
+ def suppress_app_logs(config)
+ if configured_to_suppress_app_logs?(config)
require 'logstasher/rails_ext/rack/logger'
LogStasher.remove_existing_log_subscriptions
end
end
- def configured_to_suppress_app_logs?(app)
+ def configured_to_suppress_app_logs?(config)
# This supports both spellings: "suppress_app_log" and "supress_app_log"
- !!(app.config.logstasher.suppress_app_log.nil? ? app.config.logstasher.supress_app_log : app.config.logstasher.suppress_app_log)
+ !!(config.suppress_app_log.nil? ? config.supress_app_log : config.suppress_app_log)
end
def custom_fields
Thread.current[:logstasher_custom_fields] ||= []
end
@@ -125,11 +162,11 @@
self.logger << build_logstash_event(data, ['log']).to_json + "\n"
end
end
def build_logstash_event(data, tags)
- LogStash::Event.new(data.merge('source' => self.source, 'tags' => tags))
+ ::LogStash::Event.new(data.merge('source' => self.source, 'tags' => tags))
end
def store
if RequestStore.store[STORE_KEY].nil?
# Get each store it's own private Hash instance.
@@ -142,11 +179,11 @@
RequestStore.store[REQUEST_CONTEXT_KEY] ||= {}
end
def watch(event, opts = {}, &block)
event_group = opts[:event_group] || event
- ActiveSupport::Notifications.subscribe(event) do |*args|
+ ::ActiveSupport::Notifications.subscribe(event) do |*args|
# Calling the processing block with the Notification args and the store
block.call(*args, store[event_group])
end
end
@@ -157,10 +194,10 @@
end
EOM
end
def enabled?
- self.enabled
+ self.enabled || false
end
private
def new_logger(path)