lib/logstasher.rb in logstasher-0.9.0 vs lib/logstasher.rb in logstasher-1.0.0
- old
+ new
@@ -1,11 +1,13 @@
require 'logstasher/version'
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/active_job/log_subscriber'
require 'logstasher/rails_ext/action_controller/base'
+require 'logstasher/custom_fields'
require 'request_store'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'
require 'active_support/ordered_options'
@@ -30,10 +32,12 @@
unsubscribe(:action_controller, subscriber)
when 'ActionMailer::LogSubscriber'
unsubscribe(:action_mailer, subscriber)
when 'ActiveRecord::LogSubscriber'
unsubscribe(:active_record, subscriber)
+ when 'ActiveJob::Logging::LogSubscriber'
+ unsubscribe(:active_job, subscriber)
end
end
end
def unsubscribe(component, subscriber)
@@ -49,30 +53,30 @@
def add_default_fields_to_payload(payload, request)
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]
+ LogStasher::CustomFields.add(:ip, :route, :request_id)
if self.log_controller_parameters
payload[:parameters] = payload[:params].except(*::ActionController::LogSubscriber::INTERNAL_PARAMS)
- self.custom_fields += [:parameters]
+ LogStasher::CustomFields.add(:parameters)
end
end
def add_custom_fields(&block)
wrapped_block = Proc.new do |fields|
- LogStasher.custom_fields.concat(LogStasher.store.keys)
+ LogStasher::CustomFields.add(*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)
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)
+ LogStasher::CustomFields.add(*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
@@ -89,10 +93,11 @@
self.enabled = config.enabled
LogStasher::ActiveSupport::LogSubscriber.attach_to :action_controller if config.controller_enabled
LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer if config.mailer_enabled
LogStasher::ActiveRecord::LogSubscriber.attach_to :active_record if config.record_enabled
LogStasher::ActionView::LogSubscriber.attach_to :action_view if config.view_enabled
+ LogStasher::ActiveJob::LogSubscriber.attach_to :active_job if has_active_job? && config.job_enabled
end
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
@@ -123,27 +128,23 @@
def called_as_console?
defined?(Rails::Console) && true || false
end
+ def has_active_job?
+ Rails::VERSION::MAJOR > 4 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR >= 2)
+ 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?(config)
# This supports both spellings: "suppress_app_log" and "supress_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
-
- def custom_fields=(val)
- Thread.current[:logstasher_custom_fields] = val
end
# Log an arbitrary message.
#
# Usually invoked by the level-based wrapper methods defined below.