Module: Lograge

Defined in:
lib/lograge.rb,
lib/lograge/railtie.rb,
lib/lograge/version.rb,
lib/lograge/log_subscriber.rb

Defined Under Namespace

Classes: Railtie, RequestLogSubscriber

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.custom_options(event) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/lograge.rb', line 19

def self.custom_options(event)
  if @@custom_options.respond_to?(:call)
    @@custom_options.call(event)
  else
    @@custom_options
  end
end

.remove_existing_log_subscriptionsObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/lograge.rb', line 35

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

.setup(app) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lograge.rb', line 57

def self.setup(app)
  app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
  require 'lograge/rails_ext/rack/logger'
  Lograge.remove_existing_log_subscriptions
  Lograge::RequestLogSubscriber.attach_to :action_controller
  Lograge.custom_options = app.config.lograge.custom_options
  Lograge.log_format = app.config.lograge.log_format || :lograge
  case Lograge.log_format.to_s
  when "logstash"
    begin
      require "logstash-event"
    rescue LoadError
      puts "You need to install the logstash-event gem to use the logstash output."
      raise
    end
  end
end

.unsubscribe(component, subscriber) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/lograge.rb', line 46

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