Skip to content

Commit 927586c

Browse files
committed
[test] align Rails logger initializer specs
1 parent c5838ca commit 927586c

File tree

11 files changed

+159
-44
lines changed

11 files changed

+159
-44
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
it_behaves_like "FileSystemLayout"
219219

220220
it "sets app uri from a rails.root context param" do
221-
base = File.join File.dirname(__FILE__), '../../rails3x'
221+
base = File.join File.dirname(__FILE__), '../../rails'
222222
@rack_context.should_receive(:getInitParameter).with("rails.root").and_return base
223223
expect( layout.app_uri ).to eq base
224224
expect( layout.app_path ).to eq File.expand_path(base)

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

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,10 @@
9999
rails_booter.public_path.should == "."
100100
end
101101

102-
it "uses JRuby-Rack's logger by default" do
103-
booter.boot!
104-
expect( booter.logger ).to_not be nil
105-
expect( booter.logger ).to be JRuby::Rack.logger
106-
booter.logger.info 'hello-there'
107-
end
108-
109-
RAILS_ROOT_DIR = File.expand_path("../../../rails3x", __FILE__)
102+
RAILS_ROOT_DIR = File.expand_path("../../../rails", __FILE__)
110103

111104
# NOTE: specs currently only test with a stubbed Rails::Railtie
112-
describe "Rails 3.x", :lib => :stub do
105+
describe "Rails (stubbed)", :lib => :stub do
113106

114107
before :all do
115108
$LOAD_PATH.unshift File.join(RAILS_ROOT_DIR, 'stub') # for require 'rails/railtie'
@@ -173,6 +166,21 @@
173166

174167
describe "logger" do
175168

169+
before(:all) do
170+
@active_support = defined? ::ActiveSupport
171+
@tagged_logging = active_support && ActiveSupport::TaggedLogging rescue false
172+
end
173+
174+
after(:all) do
175+
if @tagged_logging == false
176+
if @active_support
177+
ActiveSupport.send :remove_const, :TaggedLogging
178+
else
179+
Object.send :remove_const, :ActiveSupport rescue nil
180+
end
181+
end
182+
end
183+
176184
before do
177185
@app = double "app"
178186
@app.stub(:config).and_return @config = double("config")
@@ -187,17 +195,17 @@ def logger=(logger); @logger = logger; end
187195
log_initializer[1].should == [{:before => :initialize_logger}]
188196
end
189197

190-
it "gets set as config.logger" do
198+
it "gets set as config.logger (wrapped with tagged logging)" do
191199
logger = JRuby::Rack::Logger.new STDERR
192200
@config.stub(:log_level).and_return(:info)
193201
@config.stub(:log_formatter).and_return(nil)
194202

195203
JRuby::Rack.should_receive(:logger).and_return(logger)
196-
#logger.class.should_receive(:const_get).with('INFO').and_return(nil)
197-
#logger.should_receive(:level=).with(nil)
198204

199205
log_initializer.last.call(@app)
200-
@app.config.logger.should be(logger)
206+
rails_logger = @app.config.logger
207+
# ActiveSupport::TaggedLogging.new clones the original logger instance
208+
expect(rails_logger).to be_a(JRuby::Rack::Logger)
201209
end
202210

203211
it "has a configurable log level" do
@@ -206,38 +214,10 @@ def logger; @logger; end
206214
def logger=(logger); @logger = logger; end
207215
end
208216
@config.stub(:log_formatter).and_return(nil)
209-
@config.should_receive(:log_level).and_return(:debug)
217+
@config.should_receive(:log_level).and_return(:error)
210218

211219
log_initializer.last.call(@app) ##
212-
@app.config.logger.level.should be(JRuby::Rack::Logger::DEBUG)
213-
end
214-
215-
it "is wrapped in tagged logging" do # Rails 3.2
216-
active_support = defined? ::ActiveSupport
217-
tagged_logging = active_support && ActiveSupport::TaggedLogging rescue nil
218-
begin
219-
klass = Class.new do # TaggedLogging stub
220-
def initialize(logger); @logger = logger end
221-
end
222-
module ::ActiveSupport; end
223-
::ActiveSupport.const_set(:TaggedLogging, klass)
224-
@config.stub(:log_level).and_return(nil)
225-
@config.stub(:log_formatter).and_return(nil)
226-
227-
log_initializer.last.call(@app)
228-
@app.config.logger.should be_a(klass)
229-
@app.config.logger.instance_variable_get(:@logger).should be_a(JRuby::Rack::Logger)
230-
ensure
231-
if tagged_logging.nil?
232-
if active_support
233-
ActiveSupport.send :remove_const, :TaggedLogging
234-
else
235-
Object.send :remove_const, :ActiveSupport rescue nil
236-
end
237-
else
238-
ActiveSupport.const_set(:TaggedLogging, tagged_logging)
239-
end
240-
end
220+
@app.config.logger.level.should be(JRuby::Rack::Logger::ERROR)
241221
end
242222

243223
private
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# frozen_string_literal: true
2+
3+
# require "active_support/logger_silence"
4+
# require "active_support/logger_thread_safe_level"
5+
require "logger"
6+
7+
module ActiveSupport
8+
class Logger < ::Logger
9+
# include LoggerSilence
10+
11+
# Returns true if the logger destination matches one of the sources
12+
#
13+
# logger = Logger.new(STDOUT)
14+
# ActiveSupport::Logger.logger_outputs_to?(logger, STDOUT)
15+
# # => true
16+
def self.logger_outputs_to?(logger, *sources)
17+
logdev = logger.instance_variable_get(:@logdev)
18+
logger_source = logdev.dev if logdev.respond_to?(:dev)
19+
sources.any? { |source| source == logger_source }
20+
end
21+
22+
# Broadcasts logs to multiple loggers.
23+
def self.broadcast(logger) # :nodoc:
24+
Module.new do
25+
define_method(:add) do |*args, &block|
26+
logger.add(*args, &block)
27+
super(*args, &block)
28+
end
29+
30+
define_method(:<<) do |x|
31+
logger << x
32+
super(x)
33+
end
34+
35+
define_method(:close) do
36+
logger.close
37+
super()
38+
end
39+
40+
define_method(:progname=) do |name|
41+
logger.progname = name
42+
super(name)
43+
end
44+
45+
define_method(:formatter=) do |formatter|
46+
logger.formatter = formatter
47+
super(formatter)
48+
end
49+
50+
define_method(:level=) do |level|
51+
logger.level = level
52+
super(level)
53+
end
54+
55+
define_method(:local_level=) do |level|
56+
logger.local_level = level if logger.respond_to?(:local_level=)
57+
super(level) if respond_to?(:local_level=)
58+
end
59+
60+
define_method(:silence) do |level = Logger::ERROR, &block|
61+
if logger.respond_to?(:silence)
62+
logger.silence(level) do
63+
if defined?(super)
64+
super(level, &block)
65+
else
66+
block.call(self)
67+
end
68+
end
69+
else
70+
if defined?(super)
71+
super(level, &block)
72+
else
73+
block.call(self)
74+
end
75+
end
76+
end
77+
end
78+
end
79+
80+
# Simple formatter which only displays the message.
81+
class SimpleFormatter < ::Logger::Formatter
82+
# This method is invoked when a log event occurs
83+
def call(severity, timestamp, progname, msg)
84+
"#{String === msg ? msg : msg.inspect}\n"
85+
end
86+
end
87+
end
88+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
require "active_support/logger"
4+
5+
module ActiveSupport
6+
# Wraps any standard Logger object to provide tagging capabilities.
7+
#
8+
# May be called with a block:
9+
#
10+
# logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
11+
# logger.tagged('BCX') { logger.info 'Stuff' } # Logs "[BCX] Stuff"
12+
# logger.tagged('BCX', "Jason") { logger.info 'Stuff' } # Logs "[BCX] [Jason] Stuff"
13+
# logger.tagged('BCX') { logger.tagged('Jason') { logger.info 'Stuff' } } # Logs "[BCX] [Jason] Stuff"
14+
#
15+
# If called without a block, a new logger will be returned with applied tags:
16+
#
17+
# logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
18+
# logger.tagged("BCX").info "Stuff" # Logs "[BCX] Stuff"
19+
# logger.tagged("BCX", "Jason").info "Stuff" # Logs "[BCX] [Jason] Stuff"
20+
# logger.tagged("BCX").tagged("Jason").info "Stuff" # Logs "[BCX] [Jason] Stuff"
21+
#
22+
# This is used by the default Rails.logger as configured by Railties to make
23+
# it easy to stamp log lines with subdomains, request ids, and anything else
24+
# to aid debugging of multi-user production applications.
25+
module TaggedLogging
26+
module Formatter # :nodoc:
27+
# This method is invoked when a log event occurs.
28+
def call(severity, timestamp, progname, msg)
29+
super(severity, timestamp, progname, "#{tags_text}#{msg}")
30+
end
31+
end
32+
33+
def self.new(logger)
34+
logger = logger.clone
35+
36+
if logger.formatter
37+
logger.formatter = logger.formatter.dup
38+
else
39+
# Ensure we set a default formatter so we aren't extending nil!
40+
logger.formatter = ActiveSupport::Logger::SimpleFormatter.new
41+
end
42+
43+
logger.formatter.extend Formatter
44+
logger.extend(self)
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)