You can now pass hashes to the logger

This commit is contained in:
Raphael Sofaer 2011-03-09 11:48:17 -08:00
parent deeb8044f5
commit c259fc65f0
5 changed files with 55 additions and 15 deletions

View file

@ -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'

View file

@ -0,0 +1 @@
Rails.logger.class.send(:include, SplunkLogging)

View file

@ -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

21
lib/splunk_logging.rb Normal file
View file

@ -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

View file

@ -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