Module: LogStasher

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

Defined Under Namespace

Modules: Device Classes: Railtie, RequestLogSubscriber

Constant Summary collapse

VERSION =
"0.5.0"

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

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#add_custom_fields(&block) ⇒ Object



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

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



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

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)


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

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



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

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

#custom_fields=(val) ⇒ Object



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

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

#log(severity, msg) ⇒ Object



84
85
86
87
88
89
# File 'lib/logstasher.rb', line 84

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

#remove_existing_log_subscriptionsObject



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

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



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

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.source = app.config.logstasher.source unless app.config.logstasher.source.nil?
  self.enabled = true
  self.log_controller_parameters = !! app.config.logstasher.log_controller_parameters
end

#suppress_app_logs(app) ⇒ Object



64
65
66
67
68
69
# File 'lib/logstasher.rb', line 64

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



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

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