You can now pass hashes to the logger
This commit is contained in:
parent
deeb8044f5
commit
c259fc65f0
5 changed files with 55 additions and 15 deletions
|
|
@ -17,6 +17,7 @@ class ApplicationController < ActionController::Base
|
||||||
before_filter :set_grammatical_gender
|
before_filter :set_grammatical_gender
|
||||||
|
|
||||||
inflection_method :grammatical_gender => :gender
|
inflection_method :grammatical_gender => :gender
|
||||||
|
#include Oink::InstanceTypeCounter
|
||||||
|
|
||||||
def ensure_http_referer_is_set
|
def ensure_http_referer_is_set
|
||||||
request.env['HTTP_REFERER'] ||= '/aspects'
|
request.env['HTTP_REFERER'] ||= '/aspects'
|
||||||
|
|
|
||||||
1
config/initializers/_splunk_logger.rb
Normal file
1
config/initializers/_splunk_logger.rb
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Rails.logger.class.send(:include, SplunkLogging)
|
||||||
|
|
@ -39,14 +39,24 @@ class ActionController::LogSubscriber
|
||||||
additions = ActionController::Base.log_process_action(payload)
|
additions = ActionController::Base.log_process_action(payload)
|
||||||
params = payload[:params].except(*INTERNAL_PARAMS)
|
params = payload[:params].except(*INTERNAL_PARAMS)
|
||||||
|
|
||||||
log_string = "event=request_completed status=#{payload[:status]} "
|
log_hash = {:event => 'request_completed',
|
||||||
log_string << "controller=#{payload[:controller]} action=#{payload[:action]} format=#{payload[:formats].first.to_s.upcase} "
|
:status => payload[:status],
|
||||||
log_string << "ms=#{"%.0f" % event.duration} "
|
:controller => payload[:controller],
|
||||||
log_string << "gc_ms=#{GC.time/1000} gc_collections=#{GC.collections} gc_bytes=#{GC.growth} " if GC.respond_to?(:enable_stats)
|
:action => payload[:action],
|
||||||
log_string << "params='#{params.inspect}' " unless params.empty?
|
:format => payload[:formats].first.to_s.upcase,
|
||||||
#log_string << "additions='#{additions.join(" | ")}' " unless additions.blank?
|
: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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
21
lib/splunk_logging.rb
Normal file
21
lib/splunk_logging.rb
Normal 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
|
||||||
|
|
@ -1,19 +1,26 @@
|
||||||
class FakeLogger
|
class FakeLogger
|
||||||
attr_accessor :infos
|
attr_accessor :infos
|
||||||
attr_accessor :lines
|
attr_accessor :lines
|
||||||
|
attr_accessor :fatals
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
self.infos = []
|
self.infos = []
|
||||||
|
self.fatals = []
|
||||||
self.lines = []
|
self.lines = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def info line
|
def add(arg1, line, targ_arr, &block)
|
||||||
self.infos << line
|
|
||||||
self.lines << line
|
self.lines << line
|
||||||
|
targ_arr << line
|
||||||
|
end
|
||||||
|
def info line
|
||||||
|
self.add(nil, line, self.infos)
|
||||||
end
|
end
|
||||||
def fatal line
|
def fatal line
|
||||||
self.lines << line
|
self.add(nil, line, self.fatals)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
include SplunkLogging
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples_for 'it overrides the logs on success' do
|
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
|
@line = Rails.logger.infos.last
|
||||||
end
|
end
|
||||||
it 'logs the completion of a request' do
|
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
|
end
|
||||||
it 'logs an ok' do
|
it 'logs an ok' do
|
||||||
@line.include?('status=200').should be_true
|
@line.include?("status='200'").should be_true
|
||||||
end
|
end
|
||||||
it 'logs the controller' do
|
it 'logs the controller' do
|
||||||
@line.include?("controller=#{controller.class.name}").should be_true
|
@line.include?("controller='#{controller.class.name}'").should be_true
|
||||||
end
|
end
|
||||||
it 'logs the action' do
|
it 'logs the action' do
|
||||||
@line.include?("action=#{@action}").should be_true
|
@line.include?("action='#{@action}'").should be_true
|
||||||
end
|
end
|
||||||
it 'logs params' do
|
it 'logs params' do
|
||||||
if @action_params
|
if @action_params
|
||||||
|
|
@ -85,6 +92,6 @@ shared_examples_for 'it overrides the logs on redirect' do
|
||||||
@line = Rails.logger.infos.last
|
@line = Rails.logger.infos.last
|
||||||
end
|
end
|
||||||
it 'logs a redirect' do
|
it 'logs a redirect' do
|
||||||
@line.include?('status=302').should be_true
|
@line.include?("status='302'").should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue