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/ltsv.rb,
lib/lograge/ordered_options.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

Overview

rubocop:disable ModuleLength

Defined Under Namespace

Modules: Formatters Classes: OrderedOptions, Railtie, RequestLogSubscriber

Constant Summary collapse

VERSION =
'0.10.0'.freeze

Class Method Summary collapse

Class Method Details

.attach_to_action_controllerObject



141
142
143
# File 'lib/lograge.rb', line 141

def attach_to_action_controller
  Lograge::RequestLogSubscriber.attach_to :action_controller
end

.before_format(data, payload) ⇒ Object



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

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

.custom_options(event) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/lograge.rb', line 31

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

.disable_rack_cache_verbose_outputObject



176
177
178
# File 'lib/lograge.rb', line 176

def disable_rack_cache_verbose_output
  application.config.action_dispatch.rack_cache[:verbose] = false if rack_cache_hashlike?(application)
end

.extend_base_controller_class(klass) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/lograge.rb', line 159

def extend_base_controller_class(klass)
  append_payload_method = klass.instance_method(:append_info_to_payload)
  custom_payload_method = lograge_config.custom_payload_method

  klass.send(:define_method, :append_info_to_payload) do |payload|
    append_payload_method.bind(self).call(payload)
    payload[:custom_payload] = custom_payload_method.call(self)
  end
end

.ignore(test) ⇒ Object



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

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

.ignore?(event) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/lograge.rb', line 82

def 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. 'UsersController#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.



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

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

.ignore_nothingObject



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

def ignore_nothing
  @ignore_tests = []
end

.ignore_testsObject



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

def ignore_tests
  @ignore_tests ||= []
end

.keep_original_rails_logObject



180
181
182
183
184
185
# File 'lib/lograge.rb', line 180

def keep_original_rails_log
  return if lograge_config.keep_original_rails_log

  require 'lograge/rails_ext/rack/logger'
  Lograge.remove_existing_log_subscriptions
end

.lograge_configObject



204
205
206
# File 'lib/lograge.rb', line 204

def lograge_config
  application.config.lograge
end

.remove_existing_log_subscriptionsObject



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

def 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

.set_formatterObject



137
138
139
# File 'lib/lograge.rb', line 137

def set_formatter
  Lograge.formatter = lograge_config.formatter || Lograge::Formatters::KeyValue.new
end

.set_ignoresObject



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

def set_ignores
  Lograge.ignore_actions(lograge_config.ignore_actions)
  Lograge.ignore(lograge_config.ignore_custom)
end

.set_lograge_log_optionsObject



169
170
171
172
173
174
# File 'lib/lograge.rb', line 169

def set_lograge_log_options
  Lograge.logger = lograge_config.logger
  Lograge.custom_options = lograge_config.custom_options
  Lograge.before_format = lograge_config.before_format
  Lograge.log_level = lograge_config.log_level || :info
end

.setup(app) ⇒ Object



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

def setup(app)
  self.application = app
  disable_rack_cache_verbose_output
  keep_original_rails_log

  attach_to_action_controller
  set_lograge_log_options
  setup_custom_payload
  support_deprecated_config # TODO: Remove with version 1.0
  set_formatter
  set_ignores
end

.setup_custom_payloadObject



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lograge.rb', line 145

def setup_custom_payload
  return unless lograge_config.custom_payload_method.respond_to?(:call)

  base_controller_classes = Array(lograge_config.base_controller_class)
  base_controller_classes.map! { |klass| klass.try(:constantize) }
  if base_controller_classes.empty?
    base_controller_classes << ActionController::Base
  end

  base_controller_classes.each do |base_controller_class|
    extend_base_controller_class(base_controller_class)
  end
end

.support_deprecated_configObject

TODO: Remove with version 1.0



194
195
196
197
198
199
200
201
202
# File 'lib/lograge.rb', line 194

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



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

def 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