2008-01-05 08:31:04 -05:00
|
|
|
require 'abstract_unit'
|
2010-07-17 02:36:40 -04:00
|
|
|
require 'multibyte_test_helpers'
|
2007-09-25 12:48:50 -04:00
|
|
|
require 'stringio'
|
2008-03-30 21:09:19 -04:00
|
|
|
require 'fileutils'
|
2011-05-10 16:21:58 -04:00
|
|
|
require 'tempfile'
|
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
|
|
|
|
2011-05-10 16:21:58 -04:00
|
|
|
def test_write_binary_data_to_existing_file
|
|
|
|
t = Tempfile.new ['development', 'log']
|
|
|
|
t.binmode
|
|
|
|
t.write 'hi mom!'
|
|
|
|
t.close
|
|
|
|
|
2011-12-09 19:03:18 -05:00
|
|
|
f = File.open(t.path, 'w')
|
|
|
|
f.binmode
|
|
|
|
|
|
|
|
logger = Logger.new f
|
2011-05-10 16:21:58 -04:00
|
|
|
logger.level = Logger::DEBUG
|
|
|
|
|
|
|
|
str = "\x80"
|
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
|
|
|
|
fname = File.join Dir.tmpdir, 'lol', 'rofl.log'
|
2011-12-10 05:17:00 -05:00
|
|
|
FileUtils.mkdir_p File.dirname(fname)
|
2011-12-09 19:03:18 -05:00
|
|
|
f = File.open(fname, 'w')
|
|
|
|
f.binmode
|
|
|
|
|
|
|
|
logger = Logger.new f
|
2011-05-10 16:21:58 -04:00
|
|
|
logger.level = Logger::DEBUG
|
|
|
|
|
|
|
|
str = "\x80"
|
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)
|
|
|
|
assert @output.string.include?(@message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_log_debug_messages_when_log_level_is_info
|
|
|
|
@logger.level = Logger::INFO
|
|
|
|
@logger.add(Logger::DEBUG, @message)
|
|
|
|
assert ! @output.string.include?(@message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_add_message_passed_as_block_when_using_add
|
|
|
|
@logger.level = Logger::INFO
|
|
|
|
@logger.add(Logger::INFO) {@message}
|
|
|
|
assert @output.string.include?(@message)
|
|
|
|
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
|
|
|
|
@logger.info {@message}
|
|
|
|
assert @output.string.include?(@message)
|
|
|
|
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
|
|
|
|
assert @output.string.include?(@integer_message.to_s)
|
|
|
|
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
|
|
|
|
@logger.info {@integer_message}
|
|
|
|
assert @output.string.include?(@integer_message.to_s)
|
|
|
|
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
|
|
|
|
@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|
|
2011-12-19 21:41:37 -05: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
|
|
|
|
@logger.info(UNICODE_STRING)
|
|
|
|
@logger.info(BYTE_STRING)
|
|
|
|
assert @output.string.include?(UNICODE_STRING)
|
|
|
|
byte_string = @output.string.dup
|
2011-12-25 06:34:58 -05:00
|
|
|
byte_string.force_encoding("ASCII-8BIT")
|
2010-07-17 02:36:40 -04:00
|
|
|
assert byte_string.include?(BYTE_STRING)
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
assert !@output.string.include?("NOT THERE")
|
|
|
|
assert @output.string.include?("THIS IS HERE")
|
|
|
|
end
|
2007-09-25 12:48:50 -04:00
|
|
|
end
|