Skip to content

Commit 0315785

Browse files
authored
Merge pull request #309 from kares/more-logger-silence
cleanup Rails LoggerSilence support
2 parents 53b0487 + b1dd3b7 commit 0315785

File tree

5 files changed

+32
-81
lines changed

5 files changed

+32
-81
lines changed

src/main/java/org/jruby/rack/ext/Logger.java

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ public IRubyObject unknown(final ThreadContext context, final Block block) {
338338

339339
// def add(severity, message = nil, progname = nil, &block)
340340
@JRubyMethod(name = "add", required = 1, optional = 2)
341-
public IRubyObject add(final ThreadContext context,
342-
final IRubyObject[] args, final Block block) {
341+
public IRubyObject add(final ThreadContext context, final IRubyObject[] args, final Block block) {
343342
int severity = UNKNOWN;
344343
final IRubyObject sev = args[0];
345344
if ( ! sev.isNil() ) {
@@ -471,42 +470,6 @@ private void doLog(RubyString message) {
471470
logger.log( message );
472471
}
473472

474-
// LoggerSilence API :
475-
476-
private static boolean silencer = false; // we're NOT true by default!
477-
478-
@JRubyMethod(name= "silencer", meta = true)
479-
public static IRubyObject get_silencer(final ThreadContext context, final IRubyObject self) {
480-
return context.runtime.newBoolean(silencer);
481-
}
482-
483-
@JRubyMethod(name = "silencer=", meta = true)
484-
public static IRubyObject set_silencer(final ThreadContext context, final IRubyObject self,
485-
final IRubyObject value) {
486-
return context.runtime.newBoolean(silencer = value.isTrue());
487-
}
488-
489-
@JRubyMethod(name = "silence")
490-
public IRubyObject silence(final ThreadContext context, final Block block) {
491-
return doSilence(ERROR, context, block); // temp_level = Logger::ERROR
492-
}
493-
494-
@JRubyMethod(name = "silence", required = 1)
495-
public IRubyObject silence(final ThreadContext context, final IRubyObject temp_level, final Block block) {
496-
final int tempLevel = (int) temp_level.convertToInteger("to_i").getLongValue();
497-
return doSilence(tempLevel, context, block);
498-
}
499-
500-
private IRubyObject doSilence(final int tempLevel, final ThreadContext context, final Block block) {
501-
if ( silencer ) {
502-
// not implemented - on purpose!
503-
return block.yield(context, this);
504-
}
505-
else {
506-
return block.yield(context, this);
507-
}
508-
}
509-
510473
// (old) BufferedLogger API compatibility :
511474

512475
@JRubyMethod(name = "flush", alias = { "auto_flushing", "auto_flushing=" })

src/main/ruby/jruby/rack/rails/railtie.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ class Railtie < ::Rails::Railtie
3838
logger.formatter = log_formatter if log_formatter && logger.respond_to?(:formatter=)
3939
require 'active_support/tagged_logging' unless defined?(ActiveSupport::TaggedLogging)
4040
logger = ActiveSupport::TaggedLogging.new(logger) # returns a logger.clone
41-
logger.singleton_class.instance_eval do
42-
include ActiveSupport::LoggerSilence if defined?(ActiveSupport::LoggerSilence)
43-
end
41+
logger.singleton_class.send(:include, ActiveSupport::LoggerSilence) if defined?(ActiveSupport::LoggerSilence)
4442
logger
4543
end
4644
end

src/spec/ruby/jruby/rack/logger_spec.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,6 @@
102102
expect( logger.class::FATAL ).to eql 4
103103
end
104104

105-
it 'is not silencable (by default)' do
106-
expect( JRuby::Rack::Logger.silencer ).to be false
107-
end
108-
109-
it 'supports silence with block' do
110-
called = nil
111-
logger.silence do |logger|
112-
called = true
113-
expect( logger ).to be logger
114-
end
115-
expect( called ).to be true
116-
end
117-
118105
describe JRuby::Rack::ServletLog do
119106
let(:servlet_context_logger) do
120107
org.jruby.rack.logging.ServletContextLogger.new(servlet_context)

src/spec/ruby/jruby/rack/rails_booter_spec.rb

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010

1111
describe JRuby::Rack::RailsBooter do
1212

13-
let(:booter) do
14-
real_logger = org.jruby.rack.logging.BufferLogger.new
15-
JRuby::Rack.logger = JRuby::Rack::Logger.new real_logger
16-
JRuby::Rack::RailsBooter.new JRuby::Rack.context = @rack_context
13+
let(:real_logger) do
14+
org.jruby.rack.logging.BufferLogger.new
1715
end
1816

19-
let(:rails_booter) do
20-
rails_booter = booter; def rails_booter.rails2?; nil end; rails_booter
17+
let(:booter) do
18+
JRuby::Rack.logger = JRuby::Rack::Logger.new(real_logger)
19+
JRuby::Rack.context = @rack_context
20+
JRuby::Rack::RailsBooter.new @rack_context
2121
end
2222

2323
after { JRuby::Rack.context = nil; JRuby::Rack.logger = nil }
2424

2525
it "should determine RAILS_ROOT from the 'rails.root' init parameter" do
2626
@rack_context.should_receive(:getInitParameter).with("rails.root").and_return "/WEB-INF"
2727
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "./WEB-INF"
28-
rails_booter.boot!
29-
rails_booter.app_path.should == "./WEB-INF"
28+
booter.boot!
29+
booter.app_path.should == "./WEB-INF"
3030
end
3131

3232
before do
@@ -41,62 +41,62 @@
4141

4242
it "should default rails path to /WEB-INF" do
4343
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/usr/apps/WEB-INF"
44-
rails_booter.boot!
45-
rails_booter.app_path.should == "/usr/apps/WEB-INF"
44+
booter.boot!
45+
booter.app_path.should == "/usr/apps/WEB-INF"
4646
end
4747

4848
it "leaves ENV['RAILS_ENV'] as is if it was already set" do
4949
ENV['RAILS_ENV'] = 'staging'
50-
rails_booter.boot!
50+
booter.boot!
5151
ENV['RAILS_ENV'].should == 'staging'
52-
rails_booter.rails_env.should == "staging"
52+
booter.rails_env.should == "staging"
5353
end
5454

5555
it "determines RAILS_ENV from the 'rails.env' init parameter" do
5656
ENV['RAILS_ENV'] = nil
5757
@rack_context.should_receive(:getInitParameter).with("rails.env").and_return "test"
58-
rails_booter.boot!
59-
rails_booter.rails_env.should == "test"
58+
booter.boot!
59+
booter.rails_env.should == "test"
6060
end
6161

6262
it "gets rails environment from rack environmnent" do
6363
ENV.delete('RAILS_ENV')
6464
ENV['RACK_ENV'] = 'development'
6565
@rack_context.stub(:getInitParameter)
66-
rails_booter.boot!
67-
rails_booter.rails_env.should == 'development'
66+
booter.boot!
67+
booter.rails_env.should == 'development'
6868
end
6969

7070
it "default RAILS_ENV to 'production'" do
7171
ENV.delete('RAILS_ENV'); ENV.delete('RACK_ENV')
72-
rails_booter.boot!
73-
rails_booter.rails_env.should == "production"
72+
booter.boot!
73+
booter.rails_env.should == "production"
7474
end
7575

7676
it "should set RAILS_RELATIVE_URL_ROOT based on the servlet context path" do
7777
@rack_context.should_receive(:getContextPath).and_return '/myapp'
78-
rails_booter.boot!
78+
booter.boot!
7979
ENV['RAILS_RELATIVE_URL_ROOT'].should == '/myapp'
8080
end
8181

8282
it "should append to RAILS_RELATIVE_URL_ROOT if 'rails.relative_url_append' is set" do
8383
@rack_context.should_receive(:getContextPath).and_return '/myapp'
8484
@rack_context.should_receive(:getInitParameter).with("rails.relative_url_append").and_return "/blah"
85-
rails_booter.boot!
85+
booter.boot!
8686
ENV['RAILS_RELATIVE_URL_ROOT'].should == '/myapp/blah'
8787
end
8888

8989
it "should determine the public html root from the 'public.root' init parameter" do
9090
@rack_context.should_receive(:getInitParameter).with("public.root").and_return "/blah"
9191
@rack_context.should_receive(:getRealPath).with("/blah").and_return "."
92-
rails_booter.boot!
93-
rails_booter.public_path.should == "."
92+
booter.boot!
93+
booter.public_path.should == "."
9494
end
9595

9696
it "should default public root to '/'" do
9797
@rack_context.should_receive(:getRealPath).with("/").and_return "."
98-
rails_booter.boot!
99-
rails_booter.public_path.should == "."
98+
booter.boot!
99+
booter.public_path.should == "."
100100
end
101101

102102
RAILS_ROOT_DIR = File.expand_path("../../../rails", __FILE__)
@@ -211,6 +211,7 @@ def logger=(logger); @logger = logger; end
211211
expect(rails_logger).to be_a(ActiveSupport::TaggedLogging)
212212
if defined? ActiveSupport::LoggerSilence
213213
expect(rails_logger).to be_a(ActiveSupport::LoggerSilence)
214+
expect(rails_logger.silencer).to be true
214215
# sanity check silence works:
215216
value_returned = rails_logger.silence(Logger::WARN) { |logger| logger.class.name }
216217
expect(value_returned).to eql('JRuby::Rack::Logger')

src/spec/ruby/rails/stub/active_support/logger_silence.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ def self.included(base)
1212
base.class_eval do
1313
# cattr_accessor :silencer, default: true
1414
@@silencer = true
15-
def self.silencer; @@silencer end
16-
def silencer; self.class.silencer end
15+
module_eval do
16+
def self.silencer; @@silencer end
17+
def silencer; @@silencer end
18+
end
1719

1820
include ActiveSupport::LoggerThreadSafeLevel
1921
end
2022
end
2123

2224
# Silences the logger for the duration of the block.
2325
def silence(severity = Logger::ERROR)
24-
silencer ? log_at(severity) { yield self } : yield(self)
26+
silencer ? log_at(severity) { yield(self) } : yield(self)
2527
end
2628
end
2729
end

0 commit comments

Comments
 (0)