2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "multibyte_test_helpers"
|
|
|
|
require "stringio"
|
|
|
|
require "fileutils"
|
|
|
|
require "tempfile"
|
|
|
|
require "concurrent/atomics"
|
2007-09-25 12:48:50 -04:00
|
|
|
|
2012-03-31 13:10:01 -04:00
|
|
|
class LoggerTest < ActiveSupport::TestCase
|
2010-07-17 02:36:40 -04:00
|
|
|
include MultibyteTestHelpers
|
|
|
|
|
2011-12-19 21:41:37 -05:00
|
|
|
Logger = ActiveSupport::Logger
|
2009-04-22 20:41:28 -04:00
|
|
|
|
2007-09-25 12:48:50 -04:00
|
|
|
def setup
|
|
|
|
@message = "A debug message"
|
2007-09-28 10:09:35 -04:00
|
|
|
@integer_message = 12345
|
2007-09-25 12:48:50 -04:00
|
|
|
@output = StringIO.new
|
2009-04-22 20:41:28 -04:00
|
|
|
@logger = Logger.new(@output)
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2016-01-05 17:23:43 -05:00
|
|
|
def test_log_outputs_to
|
|
|
|
assert Logger.logger_outputs_to?(@logger, @output), "Expected logger_outputs_to? @output to return true but was false"
|
|
|
|
assert Logger.logger_outputs_to?(@logger, @output, STDOUT), "Expected logger_outputs_to? @output or STDOUT to return true but was false"
|
|
|
|
|
|
|
|
assert_not Logger.logger_outputs_to?(@logger, STDOUT), "Expected logger_outputs_to? to STDOUT to return false, but was true"
|
|
|
|
assert_not Logger.logger_outputs_to?(@logger, STDOUT, STDERR), "Expected logger_outputs_to? to STDOUT or STDERR to return false, but was true"
|
|
|
|
end
|
|
|
|
|
2011-05-10 16:21:58 -04:00
|
|
|
def test_write_binary_data_to_existing_file
|
2016-08-06 12:03:25 -04:00
|
|
|
t = Tempfile.new ["development", "log"]
|
2011-05-10 16:21:58 -04:00
|
|
|
t.binmode
|
2016-08-06 12:03:25 -04:00
|
|
|
t.write "hi mom!"
|
2011-05-10 16:21:58 -04:00
|
|
|
t.close
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
f = File.open(t.path, "w")
|
2011-12-09 19:03:18 -05:00
|
|
|
f.binmode
|
|
|
|
|
|
|
|
logger = Logger.new f
|
2011-05-10 16:21:58 -04:00
|
|
|
logger.level = Logger::DEBUG
|
|
|
|
|
2017-06-19 05:03:10 -04:00
|
|
|
str = "\x80".dup
|
2011-12-25 06:34:58 -05:00
|
|
|
str.force_encoding("ASCII-8BIT")
|
2011-05-10 16:21:58 -04:00
|
|
|
|
|
|
|
logger.add Logger::DEBUG, str
|
|
|
|
ensure
|
|
|
|
logger.close
|
|
|
|
t.close true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_binary_data_create_file
|
2016-08-06 12:03:25 -04:00
|
|
|
fname = File.join Dir.tmpdir, "lol", "rofl.log"
|
2011-12-10 05:17:00 -05:00
|
|
|
FileUtils.mkdir_p File.dirname(fname)
|
2016-08-06 12:03:25 -04:00
|
|
|
f = File.open(fname, "w")
|
2011-12-09 19:03:18 -05:00
|
|
|
f.binmode
|
|
|
|
|
|
|
|
logger = Logger.new f
|
2011-05-10 16:21:58 -04:00
|
|
|
logger.level = Logger::DEBUG
|
|
|
|
|
2017-06-19 05:03:10 -04:00
|
|
|
str = "\x80".dup
|
2011-12-25 06:34:58 -05:00
|
|
|
str.force_encoding("ASCII-8BIT")
|
2011-05-10 16:21:58 -04:00
|
|
|
|
|
|
|
logger.add Logger::DEBUG, str
|
|
|
|
ensure
|
|
|
|
logger.close
|
|
|
|
File.unlink fname
|
|
|
|
end
|
|
|
|
|
2007-09-25 12:48:50 -04:00
|
|
|
def test_should_log_debugging_message_when_debugging
|
|
|
|
@logger.level = Logger::DEBUG
|
|
|
|
@logger.add(Logger::DEBUG, @message)
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, @message
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_log_debug_messages_when_log_level_is_info
|
|
|
|
@logger.level = Logger::INFO
|
|
|
|
@logger.add(Logger::DEBUG, @message)
|
2016-01-05 17:23:43 -05:00
|
|
|
assert_not @output.string.include?(@message)
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_add_message_passed_as_block_when_using_add
|
|
|
|
@logger.level = Logger::INFO
|
2016-08-16 03:30:11 -04:00
|
|
|
@logger.add(Logger::INFO) { @message }
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, @message
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2007-09-25 12:48:50 -04:00
|
|
|
def test_should_add_message_passed_as_block_when_using_shortcut
|
|
|
|
@logger.level = Logger::INFO
|
2016-08-16 03:30:11 -04:00
|
|
|
@logger.info { @message }
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, @message
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2007-09-28 10:09:35 -04:00
|
|
|
def test_should_convert_message_to_string
|
|
|
|
@logger.level = Logger::INFO
|
|
|
|
@logger.info @integer_message
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, @integer_message.to_s
|
2007-09-28 10:09:35 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2007-09-28 10:09:35 -04:00
|
|
|
def test_should_convert_message_to_string_when_passed_in_block
|
|
|
|
@logger.level = Logger::INFO
|
2016-08-16 03:30:11 -04:00
|
|
|
@logger.info { @integer_message }
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, @integer_message.to_s
|
2007-09-28 10:09:35 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2007-09-25 12:48:50 -04:00
|
|
|
def test_should_not_evaluate_block_if_message_wont_be_logged
|
|
|
|
@logger.level = Logger::INFO
|
|
|
|
evaluated = false
|
2016-08-16 03:30:11 -04:00
|
|
|
@logger.add(Logger::DEBUG) { evaluated = true }
|
2007-10-03 19:43:12 -04:00
|
|
|
assert evaluated == false
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2007-09-27 00:50:58 -04:00
|
|
|
def test_should_not_mutate_message
|
|
|
|
message_copy = @message.dup
|
|
|
|
@logger.info @message
|
|
|
|
assert_equal message_copy, @message
|
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2007-10-07 14:54:36 -04:00
|
|
|
def test_should_know_if_its_loglevel_is_below_a_given_level
|
2009-04-22 20:41:28 -04:00
|
|
|
Logger::Severity.constants.each do |level|
|
2016-08-06 12:03:25 -04:00
|
|
|
next if level.to_s == "UNKNOWN"
|
2009-04-22 20:41:28 -04:00
|
|
|
@logger.level = Logger::Severity.const_get(level) - 1
|
2007-10-07 14:54:36 -04:00
|
|
|
assert @logger.send("#{level.downcase}?"), "didn't know if it was #{level.downcase}? or below"
|
|
|
|
end
|
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2010-07-17 02:36:40 -04:00
|
|
|
def test_buffer_multibyte
|
2015-06-09 22:38:53 -04:00
|
|
|
@logger.level = Logger::INFO
|
2010-07-17 02:36:40 -04:00
|
|
|
@logger.info(UNICODE_STRING)
|
|
|
|
@logger.info(BYTE_STRING)
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, UNICODE_STRING
|
2010-07-17 02:36:40 -04:00
|
|
|
byte_string = @output.string.dup
|
2011-12-25 06:34:58 -05:00
|
|
|
byte_string.force_encoding("ASCII-8BIT")
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes byte_string, BYTE_STRING
|
2010-07-17 02:36:40 -04:00
|
|
|
end
|
2015-06-09 22:38:53 -04:00
|
|
|
|
2012-12-21 13:15:36 -05:00
|
|
|
def test_silencing_everything_but_errors
|
|
|
|
@logger.silence do
|
|
|
|
@logger.debug "NOT THERE"
|
|
|
|
@logger.error "THIS IS HERE"
|
|
|
|
end
|
2015-06-09 22:38:53 -04:00
|
|
|
|
2016-01-05 17:23:43 -05:00
|
|
|
assert_not @output.string.include?("NOT THERE")
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, "THIS IS HERE"
|
2012-12-21 13:15:36 -05:00
|
|
|
end
|
2015-06-09 22:38:53 -04:00
|
|
|
|
2016-02-11 15:00:51 -05:00
|
|
|
def test_logger_silencing_works_for_broadcast
|
|
|
|
another_output = StringIO.new
|
2016-06-09 22:11:07 -04:00
|
|
|
another_logger = ActiveSupport::Logger.new(another_output)
|
2016-02-11 15:00:51 -05:00
|
|
|
|
2016-06-09 22:11:07 -04:00
|
|
|
@logger.extend ActiveSupport::Logger.broadcast(another_logger)
|
2016-02-11 15:00:51 -05:00
|
|
|
|
|
|
|
@logger.debug "CORRECT DEBUG"
|
2016-06-10 13:30:30 -04:00
|
|
|
@logger.silence do |logger|
|
|
|
|
assert_kind_of ActiveSupport::Logger, logger
|
2016-02-11 15:00:51 -05:00
|
|
|
@logger.debug "FAILURE"
|
|
|
|
@logger.error "CORRECT ERROR"
|
|
|
|
end
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, "CORRECT DEBUG"
|
|
|
|
assert_includes @output.string, "CORRECT ERROR"
|
2016-02-11 15:00:51 -05:00
|
|
|
assert_not @output.string.include?("FAILURE")
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes another_output.string, "CORRECT DEBUG"
|
|
|
|
assert_includes another_output.string, "CORRECT ERROR"
|
2016-02-11 15:00:51 -05:00
|
|
|
assert_not another_output.string.include?("FAILURE")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_broadcast_silencing_does_not_break_plain_ruby_logger
|
|
|
|
another_output = StringIO.new
|
|
|
|
another_logger = ::Logger.new(another_output)
|
|
|
|
|
2016-06-09 22:11:07 -04:00
|
|
|
@logger.extend ActiveSupport::Logger.broadcast(another_logger)
|
2016-02-11 15:00:51 -05:00
|
|
|
|
|
|
|
@logger.debug "CORRECT DEBUG"
|
2016-06-10 13:30:30 -04:00
|
|
|
@logger.silence do |logger|
|
|
|
|
assert_kind_of ActiveSupport::Logger, logger
|
2016-02-11 15:00:51 -05:00
|
|
|
@logger.debug "FAILURE"
|
|
|
|
@logger.error "CORRECT ERROR"
|
|
|
|
end
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @output.string, "CORRECT DEBUG"
|
|
|
|
assert_includes @output.string, "CORRECT ERROR"
|
2016-02-11 15:00:51 -05:00
|
|
|
assert_not @output.string.include?("FAILURE")
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes another_output.string, "CORRECT DEBUG"
|
|
|
|
assert_includes another_output.string, "CORRECT ERROR"
|
|
|
|
assert_includes another_output.string, "FAILURE"
|
2016-02-11 15:00:51 -05:00
|
|
|
# We can't silence plain ruby Logger cause with thread safety
|
|
|
|
# but at least we don't break it
|
|
|
|
end
|
|
|
|
|
2015-06-09 22:38:53 -04:00
|
|
|
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
|
|
|
|
# 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)
|
|
|
|
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
|
|
|
|
|
|
|
|
def assert_level(level)
|
|
|
|
assert_equal level, @logger.level, "Expected level #{level_name(level)}, got #{level_name(@logger.level)}"
|
|
|
|
end
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|