mirror of
https://github.com/github/rails.git
synced 2026-01-30 16:58:15 -05:00
Use non-blocking writing if available. Closes #10794 [lifofifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8638 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -39,6 +39,7 @@ module ActiveSupport
|
||||
@level = level
|
||||
@buffer = []
|
||||
@auto_flushing = 1
|
||||
@no_block = false
|
||||
|
||||
if log.respond_to?(:write)
|
||||
@log = log
|
||||
@@ -52,13 +53,19 @@ module ActiveSupport
|
||||
end
|
||||
end
|
||||
|
||||
def set_non_blocking_io
|
||||
if !RUBY_PLATFORM.match(/java|mswin/) && !(@log == STDOUT) && @log.respond_to?(:write_nonblock)
|
||||
@no_block = true
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
buffer << message
|
||||
auto_flush
|
||||
message
|
||||
end
|
||||
@@ -90,7 +97,13 @@ module ActiveSupport
|
||||
end
|
||||
|
||||
def flush
|
||||
@log.write(@buffer.slice!(0..-1).join) unless @buffer.empty?
|
||||
unless buffer.empty?
|
||||
if @no_block
|
||||
@log.write_nonblock(buffer.slice!(0..-1).join)
|
||||
else
|
||||
@log.write(buffer.slice!(0..-1).join)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def close
|
||||
@@ -101,7 +114,7 @@ module ActiveSupport
|
||||
|
||||
protected
|
||||
def auto_flush
|
||||
flush if @buffer.size >= @auto_flushing
|
||||
flush if buffer.size >= @auto_flushing
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -250,7 +250,10 @@ module Rails
|
||||
begin
|
||||
logger = ActiveSupport::BufferedLogger.new(configuration.log_path)
|
||||
logger.level = ActiveSupport::BufferedLogger.const_get(configuration.log_level.to_s.upcase)
|
||||
logger.auto_flushing = false if configuration.environment == "production"
|
||||
if configuration.environment == "production"
|
||||
logger.auto_flushing = false
|
||||
logger.set_non_blocking_io
|
||||
end
|
||||
rescue StandardError =>e
|
||||
logger = ActiveSupport::BufferedLogger.new(STDERR)
|
||||
logger.level = ActiveSupport::BufferedLogger::WARN
|
||||
|
||||
Reference in New Issue
Block a user