提交 748e6873 编写于 作者: J Jeremy Kemper

BufferedLogger#auto_flushing = N flushes the log every N messages. Buffers...

BufferedLogger#auto_flushing = N flushes the log every N messages. Buffers with an array instead of string.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7732 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 406ea8f3
*SVN*
* BufferedLogger#auto_flushing = N flushes the log every N messages. Buffers with an array instead of string. [Jeremy Kemper]
* Fixed Date#xmlschema for dates outside the range of what can be created with Time #9744 [gbuesing]
......
......@@ -29,13 +29,14 @@ def silence(temporary_level = ERROR)
end
end
attr_accessor :level, :auto_flushing
attr_accessor :level
attr_reader :auto_flushing
attr_reader :buffer
def initialize(log, level = DEBUG)
@level = level
@buffer = ""
@auto_flushing = true
@buffer = []
@auto_flushing = 1
if log.respond_to?(:write)
@log = log
......@@ -56,7 +57,7 @@ def add(severity, message = nil, progname = nil, &block)
# Ensures that the original message is not mutated.
message = "#{message}\n" unless message[-1] == ?\n
@buffer << message
flush if auto_flushing
auto_flush if auto_flushing
message
end
......@@ -72,9 +73,25 @@ def #{severity.downcase}?
EOT
end
# Set the auto-flush period. Set to true to flush after every log message,
# to an integer to flush every N messages, or to false, nil, or zero to
# never auto-flush. If you turn auto-flushing off, be sure to regularly
# flush the log yourself -- it will eat up memory until you do.
def auto_flushing=(period)
case period
when true
@auto_flushing = 1
when 0
@auto_flushing = false
when false, nil, Integer
@auto_flushing = period
else
raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
end
end
def flush
return if @buffer.size == 0
@log.write(@buffer.slice!(0..-1))
@log.write(@buffer.slice!(0..-1)) unless @buffer.empty?
end
def close
......@@ -82,5 +99,10 @@ def close
@log.close if @log.respond_to?(:close)
@log = nil
end
protected
def auto_flush
flush if @buffer.size >= @auto_flushing
end
end
end
\ No newline at end of file
end
......@@ -8,7 +8,7 @@ def setup
@output = StringIO.new
@logger = ActiveSupport::BufferedLogger.new(@output)
end
def test_should_log_debugging_message_when_debugging
@logger.level = Logger::DEBUG
@logger.add(Logger::DEBUG, @message)
......@@ -26,35 +26,62 @@ def test_should_add_message_passed_as_block_when_using_add
@logger.add(Logger::INFO) {@message}
assert @output.string.include?(@message)
end
def test_should_add_message_passed_as_block_when_using_shortcut
@logger.level = Logger::INFO
@logger.info {@message}
assert @output.string.include?(@message)
end
def test_should_convert_message_to_string
@logger.level = Logger::INFO
@logger.info @integer_message
assert @output.string.include?(@integer_message.to_s)
end
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
def test_should_not_evaluate_block_if_message_wont_be_logged
@logger.level = Logger::INFO
evaluated = false
@logger.add(Logger::DEBUG) {evaluated = true}
assert evaluated == false
assert evaluated == false
end
def test_should_not_mutate_message
message_copy = @message.dup
@logger.info @message
assert_equal message_copy, @message
end
[false, nil, 0].each do |disable|
define_method "test_disabling_auto_flush_with_#{disable.inspect}_should_buffer_until_explicit_flush" do
@logger.auto_flushing = disable
4.times do
@logger.info 'wait for it..'
assert @output.string.empty?, @output.string
end
@logger.flush
assert !@output.string.empty?, @logger.buffer.size
end
end
def test_should_auto_flush_every_n_messages
@logger.auto_flushing = 5
4.times do
@logger.info 'wait for it..'
assert @output.string.empty?, @output.string
end
@logger.info 'there it is.'
assert !@output.string.empty?, @output.string
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册