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=   buffer   clear_buffer   close   flush   new   silence  

Included Modules

Severity

Classes and Modules

Module ActiveSupport::BufferedLogger::Severity

Constants

MAX_BUFFER_SIZE = 1000

Attributes

auto_flushing  [R] 
level  [RW] 

Public Class methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 37
37:     def initialize(log, level = DEBUG)
38:       @level         = level
39:       @buffer        = {}
40:       @auto_flushing = 1
41:       @guard = Mutex.new
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:         FileUtils.mkdir_p(File.dirname(log))
50:         @log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
51:         @log.sync = true
52:         @log.write("# Logfile created on %s" % [Time.now.to_s])
53:       end
54:     end

Public Instance methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 56
56:     def add(severity, message = nil, progname = nil, &block)
57:       return if @level > severity
58:       message = (message || (block && block.call) || progname).to_s
59:       # If a newline is necessary then create a new message ending with a newline.
60:       # Ensures that the original message is not mutated.
61:       message = "#{message}\n" unless message[-1] == ?\n
62:       buffer << message
63:       auto_flush
64:       message
65:     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 84
84:     def auto_flushing=(period)
85:       @auto_flushing =
86:         case period
87:         when true;                1
88:         when false, nil, 0;       MAX_BUFFER_SIZE
89:         when Integer;             period
90:         else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
91:         end
92:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 107
107:     def close
108:       flush
109:       @log.close if @log.respond_to?(:close)
110:       @log = nil
111:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 94
 94:     def flush
 95:       @guard.synchronize do
 96:         unless buffer.empty?
 97:           old_buffer = buffer
 98:           @log.write(old_buffer.join)
 99:         end
100: 
101:         # Important to do this even if buffer was empty or else @buffer will
102:         # accumulate empty arrays for each request where nothing was logged.
103:         clear_buffer
104:       end
105:     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 114
114:       def auto_flush
115:         flush if buffer.size >= @auto_flushing
116:       end

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 118
118:       def buffer
119:         @buffer[Thread.current] ||= []
120:       end

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 122
122:       def clear_buffer
123:         @buffer.delete(Thread.current)
124:       end

[Validate]