Module: Lograge

Defined in:
lib/lograge.rb,
lib/lograge/railtie.rb,
lib/lograge/version.rb,
lib/lograge/formatters/cee.rb,
lib/lograge/formatters/raw.rb,
lib/lograge/log_subscriber.rb,
lib/lograge/formatters/graylog2.rb,
lib/lograge/formatters/logstash.rb,
lib/lograge/formatters/key_value.rb

Defined Under Namespace

Modules: Formatters Classes: Railtie, RequestLogSubscriber

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.custom_options(event) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/lograge.rb', line 25

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

.ignore(test) ⇒ Object



56
57
58
# File 'lib/lograge.rb', line 56

def self.ignore(test)
  ignore_tests.push(test) if test
end

.ignore?(event) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/lograge.rb', line 64

def self.ignore?(event)
  ignore_tests.any?{|ignore_test| ignore_test.call(event)}
end

.ignore_actions(actions) ⇒ Object

Set conditions for events that should be ignored

Currently supported formats are:

- A single string representing a controller action, e.g. 'users#sign_in'
- An array of strings representing controller actions
- An object that responds to call with an event argument and returns
  true iff the event should be ignored.

The action ignores are given to ‘ignore_actions’. The callable ignores are given to ‘ignore’. Both methods can be called multiple times, which just adds more ignore conditions to a list that is checked before logging.



45
46
47
48
49
50
# File 'lib/lograge.rb', line 45

def self.ignore_actions(actions)
  ignore(lambda do |event|
    params = event.payload[:params]
    Array(actions).include?("#{params['controller']}##{params['action']}")
  end)
end

.ignore_nothingObject



60
61
62
# File 'lib/lograge.rb', line 60

def self.ignore_nothing
  @@ignore_tests = []
end

.ignore_testsObject



52
53
54
# File 'lib/lograge.rb', line 52

def self.ignore_tests
  @@ignore_tests ||= []
end

.remove_existing_log_subscriptionsObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/lograge.rb', line 79

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



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lograge.rb', line 101

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_level = app.config.lograge.log_level || :info
  self.support_deprecated_config(app) # TODO: Remove with version 1.0
  Lograge.formatter = app.config.lograge.formatter || Lograge::Formatters::KeyValue.new
  Lograge.ignore_actions(app.config.lograge.ignore_actions)
  Lograge.ignore(app.config.lograge.ignore_custom)
end

.support_deprecated_config(app) ⇒ Object

TODO: Remove with version 1.0



115
116
117
118
119
120
121
# File 'lib/lograge.rb', line 115

def self.support_deprecated_config(app)
  if legacy_log_format = app.config.lograge.log_format
    ActiveSupport::Deprecation.warn 'config.lograge.log_format is deprecated. Use config.lograge.formatter instead.', caller
    legacy_log_format = :key_value if legacy_log_format == :lograge
    app.config.lograge.formatter = "Lograge::Formatters::#{legacy_log_format.to_s.classify}".constantize.new
  end
end

.unsubscribe(component, subscriber) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/lograge.rb', line 90

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