diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 64b9a6970..fcafebdb0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,6 +17,7 @@ class ApplicationController < ActionController::Base before_filter :set_grammatical_gender inflection_method :grammatical_gender => :gender + #include Oink::InstanceTypeCounter def ensure_http_referer_is_set request.env['HTTP_REFERER'] ||= '/aspects' diff --git a/config/initializers/_splunk_logger.rb b/config/initializers/_splunk_logger.rb new file mode 100644 index 000000000..b2d45248a --- /dev/null +++ b/config/initializers/_splunk_logger.rb @@ -0,0 +1 @@ +Rails.logger.class.send(:include, SplunkLogging) diff --git a/lib/log_overrider.rb b/lib/log_overrider.rb index 01a847af4..ea42eefdb 100644 --- a/lib/log_overrider.rb +++ b/lib/log_overrider.rb @@ -39,14 +39,24 @@ class ActionController::LogSubscriber additions = ActionController::Base.log_process_action(payload) params = payload[:params].except(*INTERNAL_PARAMS) - log_string = "event=request_completed status=#{payload[:status]} " - log_string << "controller=#{payload[:controller]} action=#{payload[:action]} format=#{payload[:formats].first.to_s.upcase} " - log_string << "ms=#{"%.0f" % event.duration} " - log_string << "gc_ms=#{GC.time/1000} gc_collections=#{GC.collections} gc_bytes=#{GC.growth} " if GC.respond_to?(:enable_stats) - log_string << "params='#{params.inspect}' " unless params.empty? - #log_string << "additions='#{additions.join(" | ")}' " unless additions.blank? + log_hash = {:event => 'request_completed', + :status => payload[:status], + :controller => payload[:controller], + :action => payload[:action], + :format => payload[:formats].first.to_s.upcase, + :ms => "%.0f" % event.duration, + :params => params.inspect} + log_hash.merge({ + :gc_ms => GC.time/1000, + :gc_collections => GC.collections, + :gc_bytes=> GC.growth}) if GC.respond_to?(:enable_stats) - Rails.logger.info(log_string) + + log_hash.merge({:params =>params.inspect}) unless params.empty? + #log_hash << "additions='#{additions.join(" | ")}' " unless additions.blank? + + + Rails.logger.info(log_hash) end end diff --git a/lib/splunk_logging.rb b/lib/splunk_logging.rb new file mode 100644 index 000000000..10008f9f4 --- /dev/null +++ b/lib/splunk_logging.rb @@ -0,0 +1,21 @@ +module SplunkLogging + def self.included(base) + base.class_eval do + alias_method_chain :add, :splunk + end + end + def add_with_splunk(arg1, log_hash = nil, arg3 = nil, &block) + add_without_splunk(arg1, format_hash(log_hash), arg3, &block) + end + def format_hash(hash) + if hash.respond_to?(:keys) + string = '' + hash.each_pair do |key, value| + string << "#{key}='#{value}' " + end + string + else + hash + end + end +end diff --git a/spec/shared_behaviors/log_override.rb b/spec/shared_behaviors/log_override.rb index f91538215..79d54baa2 100644 --- a/spec/shared_behaviors/log_override.rb +++ b/spec/shared_behaviors/log_override.rb @@ -1,19 +1,26 @@ class FakeLogger attr_accessor :infos attr_accessor :lines + attr_accessor :fatals def initialize self.infos = [] + self.fatals = [] self.lines = [] end - def info line - self.infos << line + def add(arg1, line, targ_arr, &block) self.lines << line + targ_arr << line + end + def info line + self.add(nil, line, self.infos) end def fatal line - self.lines << line + self.add(nil, line, self.fatals) end + + include SplunkLogging end shared_examples_for 'it overrides the logs on success' do @@ -34,16 +41,16 @@ shared_examples_for 'it overrides the logs on success' do @line = Rails.logger.infos.last end it 'logs the completion of a request' do - @line.include?('event=request_completed').should be_true + @line.include?("event='request_completed'").should be_true end it 'logs an ok' do - @line.include?('status=200').should be_true + @line.include?("status='200'").should be_true end it 'logs the controller' do - @line.include?("controller=#{controller.class.name}").should be_true + @line.include?("controller='#{controller.class.name}'").should be_true end it 'logs the action' do - @line.include?("action=#{@action}").should be_true + @line.include?("action='#{@action}'").should be_true end it 'logs params' do if @action_params @@ -85,6 +92,6 @@ shared_examples_for 'it overrides the logs on redirect' do @line = Rails.logger.infos.last end it 'logs a redirect' do - @line.include?('status=302').should be_true + @line.include?("status='302'").should be_true end end