Sha256: 80266755d6b9108c76068066b23e1fd840d706fa352c72ffb29d95aac3f032f6

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

require 'logstasher/version'
require 'logstasher/log_subscriber'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'
require 'active_support/ordered_options'

module Logstasher
  # Logger for the logstash logs
  mattr_accessor :logger, :enabled

  # Set the options for the adding cutom data to payload
  mattr_accessor :payload_appender

  def self.append_payload(&block)
    self.payload_appender = block
  end

  def self.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)
      end
    end
  end

  def self.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

  def self.setup(app)
    Logstasher.enabled = true
    app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
    require 'logstasher/rails_ext/rack/logger'
    require 'logstasher/rails_ext/action_controller/metal/instrumentation'
    require 'logstash/event'
    Logstasher.remove_existing_log_subscriptions
    Logstasher::RequestLogSubscriber.attach_to :action_controller
    self.logger = app.config.logstasher.logger || Logger.new("#{Rails.root}/log/logstash.log")
  end
end

require 'logstasher/railtie' if defined?(Rails)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
logstasher-0.1.1 lib/logstasher.rb
logstasher-0.1.0 lib/logstasher.rb