rework logger setup

* CommonLogger by Sinatra is no longer added if
  Rack::Server will add it. Fixes #380.

* Setting the logger options to nil will avoid
  automatically pulling in Rack::NullLogger, thus
  allowing you to set up your own logging
  middleware outside of the Sinatra middleware
  stack. Fixes #371.
This commit is contained in:
Konstantin Haase 2011-10-14 14:08:43 -07:00
parent b95764bec0
commit bf3ad8c40a
3 changed files with 35 additions and 7 deletions

View File

@ -890,6 +890,11 @@ enable it yourself:
end
end
To avoid any logging middleware to be set up, set the +logging+ setting to
+nil+. However, keep in mind that +logger+ will in that case return +nil+. A
common use case is when you want to set your own logger. Sinatra will use
whatever it will find in <tt>env['rack.logger']</tt>.
=== Mime Types
When using <tt>send_file</tt> or static files you may have mime types Sinatra

View File

@ -1353,14 +1353,27 @@ module Sinatra
def setup_logging(builder)
if logging?
builder.use Rack::CommonLogger
if logging.respond_to? :to_int
builder.use Rack::Logger, logging
else
builder.use Rack::Logger
end
setup_common_logger(builder)
setup_custom_logger(builder)
elsif logging == false
setup_null_logger(builder)
end
end
def setup_null_logger(builder)
builder.use Rack::NullLogger
end
def setup_common_logger(builder)
return if ["development", "deployment", nil].include? ENV["RACK_ENV"]
builder.use Rack::CommonLogger
end
def setup_custom_logger(builder)
if logging.respond_to? :to_int
builder.use Rack::Logger, logging
else
builder.use Rack::NullLogger
builder.use Rack::Logger
end
end

View File

@ -1711,6 +1711,16 @@ class HelpersTest < Test::Unit::TestCase
assert !io.string.include?("INFO -- : Program started")
assert !io.string.include?("WARN -- : Nothing to do")
end
it 'does not create a logger when logging is set to nil' do
mock_app do
disable :logging
get('/') { logger.inspect }
end
get '/'
assert_body 'nil'
end
end
module ::HelperOne; def one; '1'; end; end