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/lines.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.4.0'

Class Method Summary collapse

Class Method Details

.before_format(data, payload) ⇒ Object



43
44
45
46
47
# File 'lib/lograge.rb', line 43

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

.custom_options(event) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/lograge.rb', line 29

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

.ignore(test) ⇒ Object



72
73
74
# File 'lib/lograge.rb', line 72

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

.ignore?(event) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/lograge.rb', line 80

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.



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

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

.ignore_nothingObject



76
77
78
# File 'lib/lograge.rb', line 76

def ignore_nothing
  @ignore_tests = []
end

.ignore_testsObject



68
69
70
# File 'lib/lograge.rb', line 68

def ignore_tests
  @ignore_tests ||= []
end

.lograge_configObject



144
145
146
# File 'lib/lograge.rb', line 144

def lograge_config
  application.config.lograge
end

.remove_existing_log_subscriptionsObject



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

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



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

def self.setup(app)
  self.application = 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 = lograge_config.custom_options
  Lograge.before_format = lograge_config.before_format
  Lograge.log_level = lograge_config.log_level || :info
  support_deprecated_config # TODO: Remove with version 1.0
  Lograge.formatter = lograge_config.formatter || Lograge::Formatters::KeyValue.new
  Lograge.ignore_actions(lograge_config.ignore_actions)
  Lograge.ignore(lograge_config.ignore_custom)
end

.support_deprecated_configObject

TODO: Remove with version 1.0



134
135
136
137
138
139
140
141
142
# File 'lib/lograge.rb', line 134

def support_deprecated_config
  return unless lograge_config.log_format

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

.unsubscribe(component, subscriber) ⇒ Object



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

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