diff --git a/Fudgefile b/Fudgefile index 71d0be4..14814db 100644 --- a/Fudgefile +++ b/Fudgefile @@ -15,7 +15,7 @@ task_group :duplication do end task_group :complexity do - task :flog, :exclude => '^\.\/spec\/', :max => 14.6, :average => 7.2, :methods => true + task :flog, :exclude => '^\.\/spec\/', :max => 7.7, :average => 5.7, :methods => true end build :default do diff --git a/lib/logput/middleware.rb b/lib/logput/middleware.rb index 94b2fec..1dba7ab 100644 --- a/lib/logput/middleware.rb +++ b/lib/logput/middleware.rb @@ -25,17 +25,21 @@ def call(env) private def default_path_to_log_file(env) - raise Exception, 'Must specify path to log file' unless defined? Rails - - if Rails.version >= "4.0.0" - logger(env).instance_variable_get(:@logdev).instance_variable_get(:@dev).path - else - logger(env).instance_variable_get(:@logger).instance_variable_get(:@log_dest).path - end + raise Exception, 'Must specify path to Rails log file' unless defined? Rails + path(logger(env)) || raise(Exception, "#{logger(env).class} not supported.") end def logger(env) env['action_dispatch.logger'] end + + def path(logger) + case logger + when ::ActiveSupport::TaggedLogging + logger.instance_variable_get(:@logger).instance_variable_get(:@log_dest).path + when ::Logger + logger.instance_variable_get(:@logdev).filename + end + end end end diff --git a/spec/middleware_spec.rb b/spec/middleware_spec.rb index b0a107a..6554863 100644 --- a/spec/middleware_spec.rb +++ b/spec/middleware_spec.rb @@ -1,5 +1,8 @@ require 'spec_helper' +module ActiveSupport; class TaggedLogging; end; end +class Logger; end + describe Logput::Middleware do subject{ described_class.new(app, :path_to_log_file => './spec/support/test.log') } @@ -31,47 +34,39 @@ end context 'when rails is defined' do - let(:logger) { double } + let(:path) { './spec/support/test.log' } let(:logvar) { double } - let(:logdest) { double(:path => './spec/support/test.log') } + let(:logdev) { double(:filename => path) } + let(:log_dest) { double(:path => path) } - before :each do - class Rails - def self.version - @rails_version - end - - def self.version=(v) - @rails_version = v - end - end - end + before { class Rails; end } + + context 'TaggedLogging' do + let(:logger) { ::ActiveSupport::TaggedLogging.new } - context 'Rails 4' do before :each do - Rails.version = '4.0.0' - allow(logger).to receive(:instance_variable_get).with(:@logdev).and_return(logvar) - allow(logvar).to receive(:instance_variable_get).with(:@dev).and_return(logdest) + allow(logger).to receive(:instance_variable_get).with(:@logger).and_return(logvar) + allow(logvar).to receive(:instance_variable_get).with(:@log_dest).and_return(log_dest) @request = server.get('/logput', { 'action_dispatch.logger' => logger }) end it 'accesses the correct log file' do - expect(@request.status).to eq(200) + expect(server.get('/logput').status).to eq(200) end end - context 'Rails 3' do + context 'Logger' do + let(:logger) { ::Logger.new } + before :each do - Rails.version = '3.0.0' - allow(logger).to receive(:instance_variable_get).with(:@logger).and_return(logvar) - allow(logvar).to receive(:instance_variable_get).with(:@log_dest).and_return(logdest) + allow(logger).to receive(:instance_variable_get).with(:@logdev).and_return(logdev) @request = server.get('/logput', { 'action_dispatch.logger' => logger }) end it 'accesses the correct log file' do - expect(server.get('/logput').status).to eq(200) + expect(@request.status).to eq(200) end end end