Skip to content

Commit

Permalink
Merge pull request #3 from wteuber/support_multiple_loggers
Browse files Browse the repository at this point in the history
Support multiple loggers
  • Loading branch information
chrisbarber86 committed Jul 9, 2015
2 parents afbc228 + 05e90cd commit 1bfc320
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Fudgefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions lib/logput/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 18 additions & 23 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -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') }

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1bfc320

Please sign in to comment.