lib/logstasher.rb in logstasher-1.4.0 vs lib/logstasher.rb in logstasher-2.0.0
- old
+ new
@@ -16,66 +16,65 @@
extend self
STORE_KEY = :logstasher_data
REQUEST_CONTEXT_KEY = :logstasher_request_context
attr_accessor :logger, :logger_path, :enabled, :log_controller_parameters, :source, :backtrace,
- :controller_monkey_patch, :field_renaming
+ :controller_monkey_patch, :field_renaming
+
# 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|
case subscriber.class.name
- when 'ActionView::LogSubscriber'
- unsubscribe(:action_view, subscriber)
- when 'ActionController::LogSubscriber'
- 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)
+ when 'ActionView::LogSubscriber'
+ unsubscribe(:action_view, subscriber)
+ when 'ActionController::LogSubscriber'
+ 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)
- events = subscriber.public_methods(false).reject{ |method| method.to_s == 'call' }
+ 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|
- if listener.instance_variable_get('@delegate') == subscriber
- ::ActiveSupport::Notifications.unsubscribe listener
- end
+ ::ActiveSupport::Notifications.unsubscribe listener if listener.instance_variable_get('@delegate') == subscriber
end
end
end
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']
LogStasher::CustomFields.add(:ip, :route, :request_id)
- if self.log_controller_parameters
+ if log_controller_parameters
payload[:parameters] = payload[:params].except(*::ActionController::LogSubscriber::INTERNAL_PARAMS)
LogStasher::CustomFields.add(:parameters)
end
end
def add_custom_fields(&block)
- wrapped_block = Proc.new do |fields|
+ wrapped_block = proc do |fields|
LogStasher::CustomFields.add(*LogStasher.store.keys)
instance_exec(fields, &block)
end
::ActionController::Metal.send(:define_method, :logstasher_add_custom_fields_to_payload, &wrapped_block)
::ActionController::Base.send(:define_method, :logstasher_add_custom_fields_to_payload, &wrapped_block)
end
def add_custom_fields_to_request_context(&block)
- wrapped_block = Proc.new do |fields|
+ wrapped_block = proc do |fields|
instance_exec(fields, &block)
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)
@@ -99,31 +98,31 @@
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
+ 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.suppress_app_logs(config)
+ 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.logger = config.logger || new_logger(logger_path)
+ 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
+ self.log_controller_parameters = !!config.log_controller_parameters
+ self.backtrace = !!config.backtrace unless config.backtrace.nil?
+ set_data_for_rake
+ set_data_for_console
self.field_renaming = Hash(config.field_renaming)
end
def set_data_for_rake
- self.request_context['request_id'] = ::Rake.application.top_level_tasks if self.called_as_rake?
+ request_context['request_id'] = ::Rake.application.top_level_tasks if called_as_rake?
end
def set_data_for_console
- self.request_context['request_id'] = "#{Process.pid}" if self.called_as_console?
+ request_context['request_id'] = Process.pid.to_s if called_as_console?
end
def called_as_rake?
File.basename($0) == 'rake'
end
@@ -157,34 +156,34 @@
# LogStasher.info("message")
# LogStasher.info("message", tags:"tag1")
# LogStasher.info("message", tags:["tag1", "tag2"])
# LogStasher.info("message", timing:1234)
# LogStasher.info(custom1:"yes", custom2:"no")
- def log(severity, message, additional_fields={})
- if self.logger && self.logger.send("#{severity}?")
+ def log(severity, message, additional_fields = {})
+ if logger && logger.send("#{severity}?")
- data = {'level' => severity}
+ data = { 'level' => severity }
if message.respond_to?(:to_hash)
data.merge!(message.to_hash)
else
data['message'] = message
end
# tags get special handling
tags = Array(additional_fields.delete(:tags) || 'log')
data.merge!(additional_fields)
- self.logger << build_logstash_event(data, tags).to_json + "\n"
+ logger << build_logstash_event(data, tags).to_json + "\n"
end
end
def build_logstash_event(data, tags)
field_renaming.each do |old_name, new_name|
- data[new_name] = data.delete(old_name) if data.key?(old_name)
+ data[new_name] = data.delete(old_name) if data.key?(old_name)
end
- Event.new(data.merge('source' => self.source, 'tags' => tags))
+ Event.new(data.merge('source' => source, 'tags' => tags))
end
def store
if RequestStore.store[STORE_KEY].nil?
# Get each store it's own private Hash instance.
@@ -203,19 +202,19 @@
# Calling the processing block with the Notification args and the store
block.call(*args, store[event_group])
end
end
- %w( fatal error warn info debug unknown ).each do |severity|
+ %w[fatal error warn info debug unknown].each do |severity|
eval <<-EOM, nil, __FILE__, __LINE__ + 1
def #{severity}(message=nil, additional_fields={})
self.log(:#{severity}, message, additional_fields)
end
EOM
end
def enabled?
- self.enabled || false
+ enabled || false
end
private
def new_logger(path)