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/custom_fields.rb,
lib/logstasher/active_job/log_subscriber.rb,
lib/logstasher/action_view/log_subscriber.rb,
lib/logstasher/active_record/log_subscriber.rb,
lib/logstasher/active_support/log_subscriber.rb,
lib/logstasher/rails_ext/action_controller/base.rb,
lib/logstasher/active_support/mailer_log_subscriber.rb

Defined Under Namespace

Modules: ActionController, ActionView, ActiveJob, ActiveRecord, ActiveSupport, CustomFields, Device Classes: Railtie

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



19
20
21
# File 'lib/logstasher.rb', line 19

def backtrace
  @backtrace
end

#controller_monkey_patchObject

Returns the value of attribute controller_monkey_patch.



19
20
21
# File 'lib/logstasher.rb', line 19

def controller_monkey_patch
  @controller_monkey_patch
end

#enabledObject

Returns the value of attribute enabled.



19
20
21
# File 'lib/logstasher.rb', line 19

def enabled
  @enabled
end

#field_renamingObject

Returns the value of attribute field_renaming.



19
20
21
# File 'lib/logstasher.rb', line 19

def field_renaming
  @field_renaming
end

#log_controller_parametersObject

Returns the value of attribute log_controller_parameters.



19
20
21
# File 'lib/logstasher.rb', line 19

def log_controller_parameters
  @log_controller_parameters
end

#loggerObject

Returns the value of attribute logger.



19
20
21
# File 'lib/logstasher.rb', line 19

def logger
  @logger
end

#logger_pathObject

Returns the value of attribute logger_path.



19
20
21
# File 'lib/logstasher.rb', line 19

def logger_path
  @logger_path
end

#sourceObject

Returns the value of attribute source.



19
20
21
# File 'lib/logstasher.rb', line 19

def source
  @source
end

Instance Method Details

#add_custom_fields(&block) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/logstasher.rb', line 65

def add_custom_fields(&block)
  wrapped_block = Proc.new do |fields|
    LogStasher::CustomFields.add(*LogStasher.store.keys)
    instance_exec(fields, &block)
  end
  ::ActionController::Metal.send(:define_method, :logstasher_add_custom_fields_to_payload, &wrapped_block)
  ::ActionController::Base.send(:define_method, :logstasher_add_custom_fields_to_payload, &wrapped_block)
end

#add_custom_fields_to_request_context(&block) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/logstasher.rb', line 74

def add_custom_fields_to_request_context(&block)
  wrapped_block = Proc.new do |fields|
    instance_exec(fields, &block)
    LogStasher::CustomFields.add(*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



54
55
56
57
58
59
60
61
62
63
# File 'lib/logstasher.rb', line 54

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']
  LogStasher::CustomFields.add(:ip, :route, :request_id)
  if self.log_controller_parameters
    payload[:parameters] = payload[:params].except(*::ActionController::LogSubscriber::INTERNAL_PARAMS)
    LogStasher::CustomFields.add(:parameters)
  end
end

#add_default_fields_to_request_context(request) ⇒ Object



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

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

#build_logstash_event(data, tags) ⇒ Object



180
181
182
183
184
185
# File 'lib/logstasher.rb', line 180

def build_logstash_event(data, tags)
  field_renaming.each do |old_name, new_name|
      data[new_name] = data.delete(old_name) if data.key?(old_name)
  end
  ::LogStash::Event.new(data.merge('source' => self.source, 'tags' => tags))
end

#called_as_console?Boolean

Returns:

  • (Boolean)


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

def called_as_console?
  defined?(Rails::Console) && true || false
end

#called_as_rake?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/logstasher.rb', line 126

def called_as_rake?
  File.basename($0) == 'rake'
end

#clear_request_contextObject



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

def clear_request_context
  request_context.clear
end

#configured_to_suppress_app_logs?(config) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
# File 'lib/logstasher.rb', line 145

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

#default_sourceObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/logstasher/railtie.rb', line 44

def default_source
  case RUBY_PLATFORM
  when /darwin/
    # NOTE: MacOS Sierra and later are setting `.local`
    # hostnames that even as real hostnames without the `.local` part,
    # are still unresolvable. One reliable way to get an IP is to
    # get all available IP address lists and use the first one.
    # This will always be `127.0.0.1`.
    address_info = Socket.ip_address_list.first
    address_info && address_info.ip_address
  else
    IPSocket.getaddress(Socket.gethostname)
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/logstasher.rb', line 215

def enabled?
  self.enabled || false
end

#has_active_job?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/logstasher.rb', line 134

def has_active_job?
  defined?(ActiveJob)
end

#log(severity, message, additional_fields = {}) ⇒ Object

Log an arbitrary message.

Usually invoked by the level-based wrapper methods defined below.

Examples

LogStasher.info("message")
LogStasher.info("message", tags:"tag1")
LogStasher.info("message", tags:["tag1", "tag2"])
LogStasher.info("message", timing:1234)
LogStasher.info(custom1:"yes", custom2:"no")


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/logstasher.rb', line 161

def log(severity, message, additional_fields={})
  if self.logger && self.logger.send("#{severity}?")

    data = {'level' => severity}
    if message.respond_to?(:to_hash)
      data.merge!(message.to_hash)
    else
      data['message'] = message
    end

    # tags get special handling
    tags = Array(additional_fields.delete(:tags) || 'log')

    data.merge!(additional_fields)
    self.logger << build_logstash_event(data, tags).to_json + "\n"

  end
end

#process_config(config, yml_config) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/logstasher/railtie.rb', line 59

def process_config(config, yml_config)
  # Enable the logstasher logs for the current environment
  config.enabled = yml_config[:enabled] if yml_config.key? :enabled
  config.controller_enabled = yml_config[:controller_enabled] if yml_config.key? :controller_enabled
  config.mailer_enabled = yml_config[:mailer_enabled] if yml_config.key? :mailer_enabled
  config.record_enabled = yml_config[:record_enabled] if yml_config.key? :record_enabled
  config.view_enabled = yml_config[:view_enabled] if yml_config.key? :view_enabled
  config.job_enabled = yml_config[:job_enabled] if yml_config.key? :job_enabled

  # This line is optional if you do not want to suppress app logs in your <environment>.log
  config.suppress_app_log = yml_config[:suppress_app_log] if yml_config.key? :suppress_app_log

  # This line is optional, it allows you to set a custom value for the @source field of the log event
  config.source = yml_config.key?(:source) ? yml_config[:source] : default_source

  config.backtrace = yml_config[:backtrace] if yml_config.key? :backtrace
  config.logger_path = yml_config[:logger_path] if yml_config.key? :logger_path
  config.log_level = yml_config[:log_level] if yml_config.key? :log_level
end

#remove_existing_log_subscriptionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logstasher.rb', line 26

def remove_existing_log_subscriptions
  ::ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
    case subscriber.class.name
      when 'ActionView::LogSubscriber'
        unsubscribe(:action_view, subscriber)
      when 'ActionController::LogSubscriber'
        unsubscribe(:action_controller, subscriber)
      when 'ActionMailer::LogSubscriber'
        unsubscribe(:action_mailer, subscriber)
      when 'ActiveRecord::LogSubscriber'
        unsubscribe(:active_record, subscriber)
      when 'ActiveJob::Logging::LogSubscriber'
        unsubscribe(:active_job, subscriber)
    end
  end
end

#request_contextObject



195
196
197
# File 'lib/logstasher.rb', line 195

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

#set_data_for_consoleObject



122
123
124
# File 'lib/logstasher.rb', line 122

def set_data_for_console
  self.request_context['request_id'] = "#{Process.pid}" if self.called_as_console?
end

#set_data_for_rakeObject



118
119
120
# File 'lib/logstasher.rb', line 118

def set_data_for_rake
  self.request_context['request_id'] = ::Rake.application.top_level_tasks if self.called_as_rake?
end

#setup(config) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/logstasher.rb', line 101

def setup(config)
  # Path instrumentation class to insert our hook
  if (! config.controller_monkey_patch && config.controller_monkey_patch != false) || config.controller_monkey_patch == true
    require 'logstasher/rails_ext/action_controller/metal/instrumentation'
  end
  self.suppress_app_logs(config)
  self.logger_path = config.logger_path || "#{Rails.root}/log/logstash_#{Rails.env}.log"
  self.logger = config.logger || new_logger(self.logger_path)
  self.logger.level = config.log_level || Logger::WARN
  self.source = config.source unless config.source.nil?
  self.log_controller_parameters = !! config.log_controller_parameters
  self.backtrace = !! config.backtrace unless config.backtrace.nil?
  self.set_data_for_rake
  self.set_data_for_console
  self.field_renaming = Hash(config.field_renaming)
end

#setup_before(config) ⇒ Object



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

def setup_before(config)
  require 'logstash-event'
  self.enabled = config.enabled
  LogStasher::ActiveSupport::LogSubscriber.attach_to :action_controller if config.controller_enabled
  LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer if config.mailer_enabled
  LogStasher::ActiveRecord::LogSubscriber.attach_to :active_record if config.record_enabled
  LogStasher::ActionView::LogSubscriber.attach_to :action_view if config.view_enabled
  LogStasher::ActiveJob::LogSubscriber.attach_to :active_job if has_active_job? && config.job_enabled
end

#storeObject



187
188
189
190
191
192
193
# File 'lib/logstasher.rb', line 187

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(config) ⇒ Object



138
139
140
141
142
143
# File 'lib/logstasher.rb', line 138

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

#unsubscribe(component, subscriber) ⇒ Object



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

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



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

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