module LogStasher

Constants

REQUEST_CONTEXT_KEY
STORE_KEY
VERSION

Attributes

backtrace[RW]
enabled[RW]
log_controller_parameters[RW]
logger[RW]
logger_path[RW]
source[RW]

Public Instance Methods

add_custom_fields(&block) click to toggle source
# File lib/logstasher.rb, line 54
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)
end
add_custom_fields_to_request_context(&block) click to toggle source
# File lib/logstasher.rb, line 63
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
add_default_fields_to_payload(payload, request) click to toggle source
# File lib/logstasher.rb, line 43
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]
  if self.log_controller_parameters
    payload[:parameters] = payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS)
    self.custom_fields += [:parameters]
  end
end
add_default_fields_to_request_context(request) click to toggle source
# File lib/logstasher.rb, line 72
def add_default_fields_to_request_context(request)
  request_context[:request_id] = request.env['action_dispatch.request_id']
end
clear_request_context() click to toggle source
# File lib/logstasher.rb, line 76
def clear_request_context
  request_context.clear
end
configured_to_suppress_app_logs?(app) click to toggle source
# File lib/logstasher.rb, line 104
def configured_to_suppress_app_logs?(app)
  # 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)
end
custom_fields() click to toggle source
# File lib/logstasher.rb, line 109
def custom_fields
  Thread.current[:logstasher_custom_fields] ||= []
end
custom_fields=(val) click to toggle source
# File lib/logstasher.rb, line 113
def custom_fields=(val)
  Thread.current[:logstasher_custom_fields] = val
end
enabled?() click to toggle source
# File lib/logstasher.rb, line 152
def enabled?
  self.enabled
end
log(severity, msg) click to toggle source
# File lib/logstasher.rb, line 117
def log(severity, msg)
  if self.logger && self.logger.send("#{severity}?")
    event = LogStash::Event.new('@source' => self.source, '@fields' => {:message => msg, :level => severity}, '@tags' => ['log'])
    self.logger << event.to_json + "\n"
  end
end
remove_existing_log_subscriptions() click to toggle source
# File lib/logstasher.rb, line 19
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)
    end
  end
end
request_context() click to toggle source
# File lib/logstasher.rb, line 132
def request_context
  RequestStore.store[REQUEST_CONTEXT_KEY] ||= {}
end
setup(app) click to toggle source
# File lib/logstasher.rb, line 80
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
  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?
end
store() click to toggle source
# File lib/logstasher.rb, line 124
def store
  if RequestStore.store[STORE_KEY].nil?
    # Get each store it's own private Hash instance.
    RequestStore.store[STORE_KEY] = Hash.new { |hash, key| hash[key] = {} }
  end
  RequestStore.store[STORE_KEY]
end
suppress_app_logs(app) click to toggle source
# File lib/logstasher.rb, line 97
def suppress_app_logs(app)
  if configured_to_suppress_app_logs?(app)
    require 'logstasher/rails_ext/rack/logger'
    LogStasher.remove_existing_log_subscriptions
  end
end
unsubscribe(component, subscriber) click to toggle source
# File lib/logstasher.rb, line 32
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|
      if listener.instance_variable_get('@delegate') == subscriber
        ActiveSupport::Notifications.unsubscribe listener
      end
    end
  end
end
watch(event, opts = {}, &block) click to toggle source
# File lib/logstasher.rb, line 136
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])
  end
end

Private Instance Methods

new_logger(path) click to toggle source
# File lib/logstasher.rb, line 158
def new_logger(path)
  FileUtils.touch path # prevent autocreate messages in log
  Logger.new path
end