Module: LogStasher

Extended by:
LogStasher
Included in:
LogStasher
Defined in:
lib/logstasher.rb,
lib/logstasher/railtie.rb,
lib/logstasher/version.rb,
lib/logstasher/log_subscriber.rb

Defined Under Namespace

Classes: Railtie, RequestLogSubscriber

Constant Summary collapse

VERSION =
"0.4.8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



9
10
11
# File 'lib/logstasher.rb', line 9

def enabled
  @enabled
end

#log_controller_parametersObject

Returns the value of attribute log_controller_parameters.



9
10
11
# File 'lib/logstasher.rb', line 9

def log_controller_parameters
  @log_controller_parameters
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/logstasher.rb', line 9

def logger
  @logger
end

Instance Method Details

#add_custom_fields(&block) ⇒ Object



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

def add_custom_fields(&block)
  ActionController::Metal.send(:define_method, :logtasher_add_custom_fields_to_payload, &block)
  ActionController::Base.send(:define_method, :logtasher_add_custom_fields_to_payload, &block)
end

#add_default_fields_to_payload(payload, request) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/logstasher.rb', line 33

def add_default_fields_to_payload(payload, request)
  payload[:ip] = request.remote_ip
  payload[:route] = "#{request.params[:controller]}##{request.params[:action]}"
  self.custom_fields += [:ip, :route]
  if self.log_controller_parameters
    payload[:parameters] = payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS)
    self.custom_fields += [:parameters]
  end
end

#configured_to_suppress_app_logs?(app) ⇒ Boolean

Returns:

  • (Boolean)


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

def configured_to_suppress_app_logs?(app)
  # This supports both spellings: "suppress_app_log" and "supress_app_log"
  !!(app.config.logstasher.suppress_app_log.nil? ? app.config.logstasher.supress_app_log : app.config.logstasher.suppress_app_log)
end

#custom_fieldsObject



73
74
75
# File 'lib/logstasher.rb', line 73

def custom_fields
  Thread.current[:logstasher_custom_fields] ||= []
end

#custom_fields=(val) ⇒ Object



77
78
79
# File 'lib/logstasher.rb', line 77

def custom_fields=(val)
  Thread.current[:logstasher_custom_fields] = val
end

#log(severity, msg) ⇒ Object



82
83
84
85
86
87
# File 'lib/logstasher.rb', line 82

def log(severity, msg)
  if self.logger && self.logger.send("#{severity}?")
    event = LogStash::Event.new('@fields' => {:message => msg, :level => severity},'@tags' => ['log'])
    self.logger.send severity, event.to_json
  end
end

#remove_existing_log_subscriptionsObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/logstasher.rb', line 11

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

#setup(app) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/logstasher.rb', line 48

def setup(app)
  app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
  # Path instrumentation class to insert our hook
  require 'logstasher/rails_ext/action_controller/metal/instrumentation'
  require 'logstash-event'
  self.suppress_app_logs(app)
  LogStasher::RequestLogSubscriber.attach_to :action_controller
  self.logger = app.config.logstasher.logger || new_logger("#{Rails.root}/log/logstash_#{Rails.env}.log")
  self.logger.level = app.config.logstasher.log_level || Logger::WARN
  self.enabled = true
  self.log_controller_parameters = !! app.config.logstasher.log_controller_parameters
end

#suppress_app_logs(app) ⇒ Object



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

def suppress_app_logs(app)
  if configured_to_suppress_app_logs?(app)
    require 'logstasher/rails_ext/rack/logger'
    LogStasher.remove_existing_log_subscriptions
  end
end

#unsubscribe(component, subscriber) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/logstasher.rb', line 22

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