module LogStasher

Constants

REQUEST_CONTEXT_KEY
STORE_KEY
VERSION

Attributes

backtrace[RW]
controller_monkey_patch[RW]
enabled[RW]
field_renaming[RW]
log_controller_parameters[RW]
logger[RW]
logger_path[RW]
source[RW]

Public Instance Methods

add_custom_fields(&block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/logstasher.rb, line 130
def called_as_console?
  defined?(Rails::Console) && true || false
end
called_as_rake?() click to toggle source
# File lib/logstasher.rb, line 126
def called_as_rake?
  File.basename($0) == 'rake'
end
clear_request_context() click to toggle source
# File lib/logstasher.rb, line 87
def clear_request_context
  request_context.clear
end
configured_to_suppress_app_logs?(config) click to toggle source
# 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_source() click to toggle source
# 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?() click to toggle source
# File lib/logstasher.rb, line 215
def enabled?
  self.enabled || false
end
has_active_job?() click to toggle source
# File lib/logstasher.rb, line 134
def has_active_job?
  Rails::VERSION::MAJOR > 4 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR >= 2)
end
log(severity, message, additional_fields={}) click to toggle source

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")
# 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) click to toggle source
# 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_subscriptions() click to toggle source
# 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_context() click to toggle source
# File lib/logstasher.rb, line 195
def request_context
  RequestStore.store[REQUEST_CONTEXT_KEY] ||= {}
end
set_data_for_console() click to toggle source
# 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_rake() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
store() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

new_logger(path) click to toggle source
# File lib/logstasher.rb, line 221
def new_logger(path)
  if path.is_a? String
    FileUtils.touch path # prevent autocreate messages in log
  end
  Logger.new path
end