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/json.rb,
lib/lograge/formatters/l2met.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.3.0"

Class Method Summary collapse

Class Method Details

.before_format(data, payload) ⇒ Object



41
42
43
44
45
# File 'lib/lograge.rb', line 41

def self.before_format(data, payload)
  result = nil
  result = @@before_format.call(data, payload) if @@before_format
  result || data
end

.custom_options(event) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/lograge.rb', line 27

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

.ignore(test) ⇒ Object



70
71
72
# File 'lib/lograge.rb', line 70

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

.ignore?(event) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/lograge.rb', line 78

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.



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

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

.ignore_nothingObject



74
75
76
# File 'lib/lograge.rb', line 74

def self.ignore_nothing
  @@ignore_tests = []
end

.ignore_testsObject



66
67
68
# File 'lib/lograge.rb', line 66

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

.remove_existing_log_subscriptionsObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/lograge.rb', line 93

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



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/lograge.rb', line 115

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.before_format = app.config.lograge.before_format
  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



130
131
132
133
134
135
136
# File 'lib/lograge.rb', line 130

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



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

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