Module: Lograge

Defined in:
lib/lograge.rb,
lib/lograge/railtie.rb,
lib/lograge/version.rb,
lib/lograge/silent_logger.rb,
lib/lograge/formatters/cee.rb,
lib/lograge/formatters/raw.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,
lib/lograge/log_subscribers/base.rb,
lib/lograge/formatters/key_value_deep.rb,
lib/lograge/log_subscribers/action_cable.rb,
lib/lograge/log_subscribers/action_controller.rb,
lib/lograge/formatters/helpers/method_and_path.rb,
lib/lograge/rails_ext/action_cable/channel/base.rb,
lib/lograge/rails_ext/action_cable/connection/base.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Modules: ActionCable, Formatters, LogSubscribers Classes: OrderedOptions, Railtie, SilentLogger

Constant Summary collapse

VERSION =
'0.14.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attach_to_action_cableObject



157
158
159
160
161
162
# File 'lib/lograge.rb', line 157

def attach_to_action_cable
  require 'lograge/rails_ext/action_cable/channel/base'
  require 'lograge/rails_ext/action_cable/connection/base'

  Lograge::LogSubscribers::ActionCable.attach_to :action_cable
end

.attach_to_action_controllerObject



153
154
155
# File 'lib/lograge.rb', line 153

def attach_to_action_controller
  Lograge::LogSubscribers::ActionController.attach_to :action_controller
end

.before_format(data, payload) ⇒ Object



53
54
55
56
57
# File 'lib/lograge.rb', line 53

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

.controller_field(params) ⇒ Object



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

def controller_field(params)
  params[:controller] || params[:channel_class] || params[:connection_class]
end

.custom_options(event) ⇒ Object



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

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

.deprecatorObject



228
229
230
# File 'lib/lograge.rb', line 228

def deprecator
  @deprecator ||= ActiveSupport::Deprecation.new('1.0', 'Lograge')
end

.disable_rack_cache_verbose_outputObject



193
194
195
# File 'lib/lograge.rb', line 193

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

.extend_base_class(klass) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/lograge.rb', line 176

def extend_base_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



86
87
88
# File 'lib/lograge.rb', line 86

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

.ignore?(event) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/lograge.rb', line 94

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.



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

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

.ignore_nothingObject



90
91
92
# File 'lib/lograge.rb', line 90

def ignore_nothing
  @ignore_tests = []
end

.ignore_testsObject



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

def ignore_tests
  @ignore_tests ||= []
end

.keep_original_rails_logObject



197
198
199
200
201
202
203
204
205
# File 'lib/lograge.rb', line 197

def keep_original_rails_log
  return if lograge_config.keep_original_rails_log

  require 'lograge/rails_ext/rack/logger'

  require 'lograge/rails_ext/action_cable/server/base' if defined?(ActionCable)

  Lograge.remove_existing_log_subscriptions
end

.lograge_configObject



224
225
226
# File 'lib/lograge.rb', line 224

def lograge_config
  application.config.lograge
end

.remove_existing_log_subscriptionsObject



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

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



149
150
151
# File 'lib/lograge.rb', line 149

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

.set_ignoresObject



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

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

.set_lograge_log_optionsObject



186
187
188
189
190
191
# File 'lib/lograge.rb', line 186

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/lograge.rb', line 129

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

  attach_to_action_controller
  attach_to_action_cable if defined?(ActionCable)

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

.setup_custom_payloadObject



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/lograge.rb', line 164

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

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

  base_classes.each do |base_class|
    extend_base_class(base_class)
  end
end

.support_deprecated_configObject

TODO: Remove with version 1.0



214
215
216
217
218
219
220
221
222
# File 'lib/lograge.rb', line 214

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.'
  deprecator.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



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

def unsubscribe(component, subscriber)
  events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' }
  events.each do |event|
    Lograge.notification_listeners_for("#{event}.#{component}").each do |listener|
      ActiveSupport::Notifications.unsubscribe listener if listener.instance_variable_get('@delegate') == subscriber
    end
  end
end

Instance Method Details

#notification_listeners_for(name) ⇒ Object



234
235
236
# File 'lib/lograge.rb', line 234

def notification_listeners_for(name)
  ActiveSupport::Notifications.notifier.all_listeners_for(name)
end