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

STORE_KEY =
:logstasher_data
VERSION =
"0.5.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



12
13
14
# File 'lib/logstasher.rb', line 12

def enabled
  @enabled
end

#log_controller_parametersObject

Returns the value of attribute log_controller_parameters.



12
13
14
# File 'lib/logstasher.rb', line 12

def log_controller_parameters
  @log_controller_parameters
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/logstasher.rb', line 12

def logger
  @logger
end

#sourceObject

Returns the value of attribute source.



12
13
14
# File 'lib/logstasher.rb', line 12

def source
  @source
end

Instance Method Details

#add_custom_fields(&block) ⇒ Object



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

def add_custom_fields(&block)
  wrapped_block = Proc.new do |fields|
    LogStasher.custom_fields.concat(LogStasher.store.keys)
    instance_exec(fields, &block)
  end
  ActionController::Metal.send(:define_method, :logtasher_add_custom_fields_to_payload, &wrapped_block)
  ActionController::Base.send(:define_method, :logtasher_add_custom_fields_to_payload, &wrapped_block)
end

#add_default_fields_to_payload(payload, request) ⇒ Object



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

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)


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

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



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

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

#custom_fields=(val) ⇒ Object



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

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

#log(severity, msg) ⇒ Object



91
92
93
94
95
96
# File 'lib/logstasher.rb', line 91

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



16
17
18
19
20
21
22
23
24
25
# File 'lib/logstasher.rb', line 16

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



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/logstasher.rb', line 57

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

#storeObject



98
99
100
101
102
103
104
# File 'lib/logstasher.rb', line 98

def store
  if RequestStore.store[STORE_KEY].nil?
    # Get each store it's own private Hash instance.
    RequestStore.store[STORE_KEY] = Hash.new { |hash, key| hash[key] = {} }
  end
  RequestStore.store[STORE_KEY]
end

#suppress_app_logs(app) ⇒ Object



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

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/logstasher.rb', line 27

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

#watch(event, opts = {}, &block) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/logstasher.rb', line 106

def watch(event, opts = {}, &block)
  event_group = opts[:event_group] || event
  ActiveSupport::Notifications.subscribe(event) do |*args|
    # Calling the processing block with the Notification args and the store
    block.call(*args, store[event_group])
  end
end