Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7732

Show
Ignore:
Timestamp:
10/03/07 23:43:12 (1 year ago)
Author:
bitsweat
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r7707 r7732  
    11*SVN* 
     2 
     3* BufferedLogger#auto_flushing = N flushes the log every N messages. Buffers with an array instead of string.  [Jeremy Kemper] 
    24 
    35* Fixed Date#xmlschema for dates outside the range of what can be created with Time #9744 [gbuesing] 
  • trunk/activesupport/lib/active_support/buffered_logger.rb

    r7664 r7732  
    3030    end 
    3131 
    32     attr_accessor :level, :auto_flushing 
     32    attr_accessor :level 
     33    attr_reader :auto_flushing 
    3334    attr_reader :buffer 
    3435 
    3536    def initialize(log, level = DEBUG) 
    3637      @level         = level 
    37       @buffer        = "" 
    38       @auto_flushing = true 
     38      @buffer        = [] 
     39      @auto_flushing = 1 
    3940 
    4041      if log.respond_to?(:write) 
     
    5758      message = "#{message}\n" unless message[-1] == ?\n 
    5859      @buffer << message 
    59       flush if auto_flushing 
     60      auto_flush if auto_flushing 
    6061      message 
    6162    end 
     
    7374    end 
    7475 
     76    # Set the auto-flush period. Set to true to flush after every log message, 
     77    # to an integer to flush every N messages, or to false, nil, or zero to 
     78    # never auto-flush. If you turn auto-flushing off, be sure to regularly 
     79    # flush the log yourself -- it will eat up memory until you do. 
     80    def auto_flushing=(period) 
     81      case period 
     82      when true 
     83        @auto_flushing = 1 
     84      when 0 
     85        @auto_flushing = false 
     86      when false, nil, Integer 
     87        @auto_flushing = period 
     88      else 
     89        raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}" 
     90      end 
     91    end 
     92 
    7593    def flush 
    76       return if @buffer.size == 0 
    77       @log.write(@buffer.slice!(0..-1)) 
     94      @log.write(@buffer.slice!(0..-1)) unless @buffer.empty? 
    7895    end 
    7996 
     
    83100      @log = nil 
    84101    end 
     102 
     103    protected 
     104      def auto_flush 
     105        flush if @buffer.size >= @auto_flushing 
     106      end 
    85107  end 
    86108end 
  • trunk/activesupport/test/buffered_logger_test.rb

    r7664 r7732  
    99    @logger  = ActiveSupport::BufferedLogger.new(@output) 
    1010  end 
    11    
     11 
    1212  def test_should_log_debugging_message_when_debugging 
    1313    @logger.level = Logger::DEBUG 
     
    2727    assert @output.string.include?(@message) 
    2828  end 
    29    
     29 
    3030  def test_should_add_message_passed_as_block_when_using_shortcut 
    3131    @logger.level = Logger::INFO 
     
    3333    assert @output.string.include?(@message) 
    3434  end 
    35    
     35 
    3636  def test_should_convert_message_to_string 
    3737    @logger.level = Logger::INFO 
     
    3939    assert @output.string.include?(@integer_message.to_s) 
    4040  end 
    41    
     41 
    4242  def test_should_convert_message_to_string_when_passed_in_block 
    4343    @logger.level = Logger::INFO 
     
    4545    assert @output.string.include?(@integer_message.to_s) 
    4646  end 
    47    
     47 
    4848  def test_should_not_evaluate_block_if_message_wont_be_logged 
    4949    @logger.level = Logger::INFO 
    5050    evaluated = false 
    5151    @logger.add(Logger::DEBUG) {evaluated = true} 
    52     assert evaluated == false   
     52    assert evaluated == false 
    5353  end 
    54    
     54 
    5555  def test_should_not_mutate_message 
    5656    message_copy = @message.dup 
     
    5858    assert_equal message_copy, @message 
    5959  end 
     60 
     61 
     62  [false, nil, 0].each do |disable| 
     63    define_method "test_disabling_auto_flush_with_#{disable.inspect}_should_buffer_until_explicit_flush" do 
     64      @logger.auto_flushing = disable 
     65 
     66      4.times do 
     67        @logger.info 'wait for it..' 
     68        assert @output.string.empty?, @output.string 
     69      end 
     70 
     71      @logger.flush 
     72      assert !@output.string.empty?, @logger.buffer.size 
     73    end 
     74  end 
     75 
     76  def test_should_auto_flush_every_n_messages 
     77    @logger.auto_flushing = 5 
     78 
     79    4.times do 
     80      @logger.info 'wait for it..' 
     81      assert @output.string.empty?, @output.string 
     82    end 
     83 
     84    @logger.info 'there it is.' 
     85    assert !@output.string.empty?, @output.string 
     86  end 
    6087end