Permalink
Browse files

simplifying buffered logger for easier and slightly more performant c…

…odes. pumpkin
  • Loading branch information...
1 parent 849007f commit a2b1be28c330b0b5e11a076830fb822b663562d1 @tenderlove tenderlove committed Apr 26, 2011
@@ -41,7 +41,7 @@ def silence(temporary_level = ERROR)
def initialize(log, level = DEBUG)
@level = level
- @buffer = {}
+ @buffer = Hash.new { |h,k| h[k] = [] }
@auto_flushing = 1
@guard = Mutex.new
@@ -100,13 +100,8 @@ def auto_flushing=(period)
def flush
@guard.synchronize do
- unless buffer.empty?
- old_buffer = buffer
- all_content = StringIO.new
- old_buffer.each do |content|
- all_content << content
- end
- @log.write(all_content.string)
+ buffer.each do |content|
+ @log.write(content)
end
# Important to do this even if buffer was empty or else @buffer will
@@ -127,7 +122,7 @@ def auto_flush
end
def buffer
- @buffer[Thread.current] ||= []
+ @buffer[Thread.current]
end
def clear_buffer
@@ -81,7 +81,7 @@ def flushable_loggers
# Flush all log_subscribers' logger.
def flush_all!
- flushable_loggers.each(&:flush)
+ flushable_loggers.each { |log| log.flush }
end
end

0 comments on commit a2b1be2

Please sign in to comment.