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

Inspired by the buffered logger idea by Ezra

Methods

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:       @no_block = false
43: 
44:       if log.respond_to?(:write)
45:         @log = log
46:       elsif File.exist?(log)
47:         @log = open(log, (File::WRONLY | File::APPEND))
48:         @log.sync = true
49:       else
50:         FileUtils.mkdir_p(File.dirname(log))
51:         @log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
52:         @log.sync = true
53:         @log.write("# Logfile created on %s" % [Time.now.to_s])
54:       end
55:     end

Public Instance methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 63
63:     def add(severity, message = nil, progname = nil, &block)
64:       return if @level > severity
65:       message = (message || (block && block.call) || progname).to_s
66:       # If a newline is necessary then create a new message ending with a newline.
67:       # Ensures that the original message is not mutated.
68:       message = "#{message}\n" unless message[-1] == ?\n
69:       buffer << message
70:       auto_flush
71:       message
72:     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 91
91:     def auto_flushing=(period)
92:       @auto_flushing =
93:         case period
94:         when true;                1
95:         when false, nil, 0;       MAX_BUFFER_SIZE
96:         when Integer;             period
97:         else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
98:         end
99:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 111
111:     def close
112:       flush
113:       @log.close if @log.respond_to?(:close)
114:       @log = nil
115:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 101
101:     def flush
102:       unless buffer.empty?
103:         if @no_block
104:           @log.write_nonblock(buffer.slice!(0..-1).join)
105:         else
106:           @log.write(buffer.slice!(0..-1).join)
107:         end
108:       end
109:     end

[Source]

    # File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 57
57:     def set_non_blocking_io
58:       if !RUBY_PLATFORM.match(/java|mswin/) && !(@log == STDOUT) && @log.respond_to?(:write_nonblock)
59:         @no_block = true
60:       end
61:     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 118
118:       def auto_flush
119:         flush if buffer.size >= @auto_flushing
120:       end

[Validate]