Class ActiveSupport::BufferedLogger
In: vendor/rails/activesupport/lib/active_support/buffered_logger.rb
Parent: Object

Inspired by the buffered logger idea by Ezra

Methods

add   auto_flush   auto_flushing=   close   flush   new   silence  

Included Modules

Severity

Classes and Modules

Module ActiveSupport::BufferedLogger::Severity

Constants

MAX_BUFFER_SIZE = 1000

Attributes

auto_flushing  [R] 
buffer  [R] 
level  [RW] 

Public Class methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 38
38:     def initialize(log, level = DEBUG)
39:       @level         = level
40:       @buffer        = []
41:       @auto_flushing = 1
42: 
43:       if log.respond_to?(:write)
44:         @log = log
45:       elsif File.exist?(log)
46:         @log = open(log, (File::WRONLY | File::APPEND))
47:         @log.sync = true
48:       else
49:         @log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
50:         @log.sync = true
51:         @log.write("# Logfile created on %s" % [Time.now.to_s])
52:       end
53:     end

Public Instance methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 55
55:     def add(severity, message = nil, progname = nil, &block)
56:       return if @level > severity
57:       message = (message || (block && block.call) || progname).to_s
58:       # If a newline is necessary then create a new message ending with a newline.
59:       # Ensures that the original message is not mutated.
60:       message = "#{message}\n" unless message[-1] == ?\n
61:       @buffer << message
62:       auto_flush
63:       message
64:     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.

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 83
83:     def auto_flushing=(period)
84:       @auto_flushing =
85:         case period
86:         when true;                1
87:         when false, nil, 0;       MAX_BUFFER_SIZE
88:         when Integer;             period
89:         else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
90:         end
91:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 97
 97:     def close
 98:       flush
 99:       @log.close if @log.respond_to?(:close)
100:       @log = nil
101:     end

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 93
93:     def flush
94:       @log.write(@buffer.slice!(0..-1).to_s) unless @buffer.empty?
95:     end

Silences the logger for the duration of the block.

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 21
21:     def silence(temporary_level = ERROR)
22:       if silencer
23:         begin
24:           old_logger_level, self.level = level, temporary_level
25:           yield self
26:         ensure
27:           self.level = old_logger_level
28:         end
29:       else
30:         yield self
31:       end
32:     end

Protected Instance methods

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 104
104:       def auto_flush
105:         flush if @buffer.size >= @auto_flushing
106:       end

[Validate]