lib/logstasher.rb in logstasher-0.1.1 vs lib/logstasher.rb in logstasher-0.2.0
- old
+ new
@@ -2,28 +2,21 @@
require 'logstasher/log_subscriber'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'
require 'active_support/ordered_options'
-module Logstasher
+module LogStasher
# Logger for the logstash logs
mattr_accessor :logger, :enabled
- # Set the options for the adding cutom data to payload
- mattr_accessor :payload_appender
-
- def self.append_payload(&block)
- self.payload_appender = block
- end
-
def self.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 ActionView::LogSubscriber
+ unsubscribe(:action_view, subscriber)
+ when ActionController::LogSubscriber
+ unsubscribe(:action_controller, subscriber)
end
end
end
def self.unsubscribe(component, subscriber)
@@ -35,18 +28,65 @@
end
end
end
end
+ def self.add_default_fields_to_payload(payload, request)
+ payload[:ip] = request.remote_ip
+ payload[:route] = "#{request.params[:controller]}##{request.params[:action]}"
+ payload[:parameters] = payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS).inject(""){|s,(k,v)|
+ s+="#{k}=#{v}\n"}
+ self.custom_fields += [:ip, :route, :parameters]
+ end
+
+ def self.add_custom_fields(&block)
+ ActionController::Base.send(:define_method, :logtasher_add_custom_fields_to_payload, &block)
+ end
+
def self.setup(app)
- Logstasher.enabled = true
app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
- require 'logstasher/rails_ext/rack/logger'
+ # Path instrumentation class to insert our hook
require 'logstasher/rails_ext/action_controller/metal/instrumentation'
require 'logstash/event'
- Logstasher.remove_existing_log_subscriptions
- Logstasher::RequestLogSubscriber.attach_to :action_controller
- self.logger = app.config.logstasher.logger || Logger.new("#{Rails.root}/log/logstash.log")
+ self.suppress_app_logs(app)
+ LogStasher::RequestLogSubscriber.attach_to :action_controller
+ self.logger = app.config.logstasher.logger || Logger.new("#{Rails.root}/log/logstash_#{Rails.env}.log")
+ self.logger.level = app.config.logstasher.log_level || Logger::WARN
+ self.enabled = true
+ self.custom_fields = []
+ end
+
+ def self.suppress_app_logs(app)
+ if app.config.logstasher.supress_app_log.nil? || app.config.logstasher.supress_app_log
+ require 'logstasher/rails_ext/rack/logger'
+ LogStasher.remove_existing_log_subscriptions
+ end
+ end
+
+ def self.custom_fields
+ Thread.current[:logstasher_custom_fields]
+ end
+
+ def self.custom_fields=(val)
+ Thread.current[:logstasher_custom_fields] = val
+ end
+
+
+ def self.log(severity, msg)
+ if self.logger && self.logger.send("#{severity}?")
+ event = LogStash::Event.new('@fields' => {:message => msg, :level => severity},'@tags' => ['log'])
+ self.logger.send severity, event.to_json
+ end
+ end
+
+ class << self
+ %w( fatal error warn info debug unknown ).each do |severity|
+ eval <<-EOM, nil, __FILE__, __LINE__ + 1
+ def #{severity}(msg)
+ self.log(:#{severity}, msg)
+ end
+ EOM
+ end
end
end
require 'logstasher/railtie' if defined?(Rails)