Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes LoggerSilence#silence threadsafety #20507

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,7 @@ def test_benchmark_with_use_silence
original_logger = ActiveRecord::Base.logger
log = StringIO.new
ActiveRecord::Base.logger = ActiveSupport::Logger.new(log)
ActiveRecord::Base.logger.level = Logger::DEBUG
ActiveRecord::Base.benchmark("Logging", :level => :debug, :silence => false) { ActiveRecord::Base.logger.debug "Quiet" }
assert_match(/Quiet/, log.string)
ensure
Expand Down
14 changes: 14 additions & 0 deletions activesupport/lib/active_support/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def initialize(*args)
super
@formatter = SimpleFormatter.new
@broadcast_messages = true
after_initialize if respond_to? :after_initialize
end

def add(severity, message = nil, progname = nil, &block)
return true if @logdev.nil? || (severity || UNKNOWN) < level
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you checking @logdev.nil?? Isn't it an i-var Rails has no control over?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActiveSupport::Logger inherits from Logger, so it does have "control" over @logdev. The reason it's checked here is because it's replicating the behavior of the super method. I personally see no problem short-circuiting here instead of adding another method call to the stack (via super) but, if you feel super strongly about it, I'll be honest - I don't care, and will remove it.

super
end

Logger::Severity.constants.each do |severity|
class_eval(<<-EOT, __FILE__, __LINE__ + 1)
def #{severity.downcase}? # def debug?
Logger::#{severity} >= level # DEBUG >= level
end # end
EOT
end

# Simple formatter which only displays the message.
Expand Down
24 changes: 22 additions & 2 deletions activesupport/lib/active_support/logger_silence.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
require 'active_support/concern'
require 'active_support/core_ext/module/attribute_accessors'
require 'concurrent'

module LoggerSilence
extend ActiveSupport::Concern

included do
cattr_accessor :silencer
attr_reader :local_levels
self.silencer = true
end

def after_initialize
@local_levels = Concurrent::Map.new(:initial_capacity => 2)
end

def local_log_id
Thread.current.__id__
end

def level
local_levels[local_log_id] || super
end

# Silences the logger for the duration of the block.
def silence(temporary_level = Logger::ERROR)
if silencer
begin
old_logger_level, self.level = level, temporary_level
old_local_level = local_levels[local_log_id]
local_levels[local_log_id] = temporary_level

yield self
ensure
self.level = old_logger_level
if old_local_level
local_levels[local_log_id] = old_local_level
else
local_levels.delete(local_log_id)
end
end
else
yield self
Expand Down
85 changes: 83 additions & 2 deletions activesupport/test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'stringio'
require 'fileutils'
require 'tempfile'
require 'concurrent/atomics'

class LoggerTest < ActiveSupport::TestCase
include MultibyteTestHelpers
Expand Down Expand Up @@ -113,21 +114,101 @@ def test_should_know_if_its_loglevel_is_below_a_given_level
end

def test_buffer_multibyte
@logger.level = Logger::INFO
@logger.info(UNICODE_STRING)
@logger.info(BYTE_STRING)
assert @output.string.include?(UNICODE_STRING)
byte_string = @output.string.dup
byte_string.force_encoding("ASCII-8BIT")
assert byte_string.include?(BYTE_STRING)
end

def test_silencing_everything_but_errors
@logger.silence do
@logger.debug "NOT THERE"
@logger.error "THIS IS HERE"
end

assert !@output.string.include?("NOT THERE")
assert @output.string.include?("THIS IS HERE")
end

def test_logger_level_per_object_thread_safety
logger1 = Logger.new(StringIO.new)
logger2 = Logger.new(StringIO.new)

level = Logger::DEBUG
assert_equal level, logger1.level, "Expected level #{level_name(level)}, got #{level_name(logger1.level)}"
assert_equal level, logger2.level, "Expected level #{level_name(level)}, got #{level_name(logger2.level)}"

logger1.level = Logger::ERROR
assert_equal level, logger2.level, "Expected level #{level_name(level)}, got #{level_name(logger2.level)}"
end

def test_logger_level_main_thread_safety
@logger.level = Logger::INFO
assert_level(Logger::INFO)

latch = Concurrent::CountDownLatch.new
latch2 = Concurrent::CountDownLatch.new

t = Thread.new do
latch.wait
assert_level(Logger::INFO)
latch2.count_down
end

@logger.silence(Logger::ERROR) do
assert_level(Logger::ERROR)
latch.count_down
latch2.wait
end

t.join
end

def test_logger_level_local_thread_safety
@logger.level = Logger::INFO
assert_level(Logger::INFO)

thread_1_latch = Concurrent::CountDownLatch.new
thread_2_latch = Concurrent::CountDownLatch.new

threads = (1..2).collect do |thread_number|
Thread.new do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still find this test with the threads hard to understand, and I'm scared we might run into timing issues (might be overblown).

Could stop be of help here? That's pretty much what you're doing with sleep.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at the rest of this PR, but you want latch.

# force thread 2 to wait until thread 1 is already in @logger.silence
thread_2_latch.wait if thread_number == 2

@logger.silence(Logger::ERROR) do
assert_level(Logger::ERROR)
@logger.silence(Logger::DEBUG) do
# allow thread 2 to finish but hold thread 1
if thread_number == 1
thread_2_latch.count_down
thread_1_latch.wait
end
assert_level(Logger::DEBUG)
end
end

# allow thread 1 to finish
assert_level(Logger::INFO)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines should be swapped, this way the check for the info logger level is done while thread 1 is still guaranteed to be in with_level(:error).

thread_1_latch.count_down if thread_number == 2
end
end

threads.each(&:join)
assert_level(Logger::INFO)
end

private
def level_name(level)
::Logger::Severity.constants.find do |severity|
Logger.const_get(severity) == level
end.to_s
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ this line and indent under private.

def assert_level(level)
assert_equal level, @logger.level, "Expected level #{level_name(level)}, got #{level_name(@logger.level)}"
end
end