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: MailerLogSubscriber, Railtie, RequestLogSubscriber

Constant Summary collapse

STORE_KEY =
:logstasher_data
REQUEST_CONTEXT_KEY =
:logstasher_request_context
VERSION =
"0.6.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

#log_controller_parametersObject

Returns the value of attribute log_controller_parameters.



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

def log_controller_parameters
  @log_controller_parameters
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#logger_pathObject

Returns the value of attribute logger_path.



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

def logger_path
  @logger_path
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#add_custom_fields(&block) ⇒ Object



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

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_custom_fields_to_request_context(&block) ⇒ Object



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

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

#add_default_fields_to_payload(payload, request) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/logstasher.rb', line 41

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

#add_default_fields_to_request_context(request) ⇒ Object



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

def add_default_fields_to_request_context(request)
  request_context[:request_id] = request.env['action_dispatch.request_id']
end

#clear_request_contextObject



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

def clear_request_context
  request_context.clear
end

#configured_to_suppress_app_logs?(app) ⇒ Boolean

Returns:

  • (Boolean)


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

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



106
107
108
# File 'lib/logstasher.rb', line 106

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

#custom_fields=(val) ⇒ Object



110
111
112
# File 'lib/logstasher.rb', line 110

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

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  self.enabled
end

#log(severity, msg) ⇒ Object



114
115
116
117
118
119
# File 'lib/logstasher.rb', line 114

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



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

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)
      when ActionMailer::LogSubscriber
        unsubscribe(:action_mailer, subscriber)
    end
  end
end

#request_contextObject



129
130
131
# File 'lib/logstasher.rb', line 129

def request_context
  RequestStore.store[REQUEST_CONTEXT_KEY] ||= {}
end

#setup(app) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/logstasher.rb', line 78

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
  LogStasher::MailerLogSubscriber.attach_to :action_mailer
  self.logger_path = app.config.logstasher.logger_path || "#{Rails.root}/log/logstash_#{Rails.env}.log"
  self.logger = app.config.logstasher.logger || new_logger(self.logger_path)
  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



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

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



94
95
96
97
98
99
# File 'lib/logstasher.rb', line 94

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



30
31
32
33
34
35
36
37
38
39
# File 'lib/logstasher.rb', line 30

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



133
134
135
136
137
138
139
# File 'lib/logstasher.rb', line 133

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