Parent

Class/Module Index [+]

Quicksearch

ActiveSupport::BufferedLogger

Inspired by the buffered logger idea by Ezra

Constants

MAX_BUFFER_SIZE

Attributes

auto_flushing[R]
level[RW]

Public Instance Methods

add(severity, message = nil, progname = nil, &block) click to toggle source
# File lib/active_support/buffered_logger.rb, line 65
def add(severity, message = nil, progname = nil, &block)
  return if @level > severity
  message = (message || (block && block.call) || progname).to_s
  # If a newline is necessary then create a new message ending with a newline.
  # Ensures that the original message is not mutated.
  message = "#{message}\n" unless message[-1] == \n\
  buffer << message
  auto_flush
  message
end
auto_flushing=(period) click to toggle source

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.

# File lib/active_support/buffered_logger.rb, line 96
def auto_flushing=(period)
  @auto_flushing =
    case period
    when true;                1
    when false, nil, 0;       MAX_BUFFER_SIZE
    when Integer;             period
    else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
    end
end
close() click to toggle source
# File lib/active_support/buffered_logger.rb, line 118
def close
  flush
  @log.close if @log.respond_to?(:close)
  @log = nil
end
flush() click to toggle source
# File lib/active_support/buffered_logger.rb, line 106
def flush
  @guard.synchronize do
    buffer.each do |content|
      @log.write(content)
    end

    # Important to do this even if buffer was empty or else @buffer will
    # accumulate empty arrays for each request where nothing was logged.
    clear_buffer
  end
end
open_log(log, mode) click to toggle source
# File lib/active_support/buffered_logger.rb, line 58
def open_log(log, mode)
  open(log, mode).tap do |open_log|
    open_log.set_encoding(Encoding::BINARY) if open_log.respond_to?(:set_encoding)
    open_log.sync = true
  end
end
silence(temporary_level = ERROR) click to toggle source

Silences the logger for the duration of the block.

# File lib/active_support/buffered_logger.rb, line 26
def silence(temporary_level = ERROR)
  if silencer
    begin
      old_logger_level, self.level = level, temporary_level
      yield self
    ensure
      self.level = old_logger_level
    end
  else
    yield self
  end
end

Protected Instance Methods

auto_flush() click to toggle source
# File lib/active_support/buffered_logger.rb, line 125
def auto_flush
  flush if buffer.size >= @auto_flushing
end
buffer() click to toggle source
# File lib/active_support/buffered_logger.rb, line 129
def buffer
  @buffer[Thread.current]
end
clear_buffer() click to toggle source
# File lib/active_support/buffered_logger.rb, line 133
def clear_buffer
  @buffer.delete(Thread.current)
end

Public Class Methods

new(log, level = DEBUG) click to toggle source
# File lib/active_support/buffered_logger.rb, line 42
def initialize(log, level = DEBUG)
  @level         = level
  @buffer        = Hash.new { |h,k| h[k] = [] }
  @auto_flushing = 1
  @guard = Mutex.new

  if log.respond_to?(:write)
    @log = log
  elsif File.exist?(log)
    @log = open_log(log, (File::WRONLY | File::APPEND))
  else
    FileUtils.mkdir_p(File.dirname(log))
    @log = open_log(log, (File::WRONLY | File::APPEND | File::CREAT))
  end
end
silencer click to toggle source

Set to false to disable the silencer

# File lib/active_support/buffered_logger.rb, line 22
cattr_accessor :silencer

[Validate]

Generated with the Darkfish Rdoc Generator 2.