1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Merge branch 'better_logging'

Conflicts:
	CHANGES
This commit is contained in:
Konstantin Haase 2011-03-21 11:52:29 +01:00
commit 7d3b99ecfe
4 changed files with 78 additions and 3 deletions

View file

@ -2,6 +2,10 @@
* Added support for HTTP PATCH requests. (Konstantin Haase)
* Set up `Rack::Logger` or `Rack::NullLogger` depending on whether logging
was enabled or not. Also, expose that logger with the `logger` helper
method. (Konstantin Haase)
* The sessions setting now may be an options hash. (Konstantin Haase)
* Important: 1.8.6 support has been dropped. It is still usable if you use the

View file

@ -912,6 +912,29 @@ Similar to the body, you can also set the status code and headers:
Like +body+, +headers+ and +status+ with no arguments can be used to access
their current values.
=== Logging
In the request scope, the +logger+ helper exposes a +Logger+ instance:
get '/' do
logger.info "loading data"
# ...
end
This logger will automatically take your Rack handler's logging settings into
account. If logging is disabled, this method will return a dummy object, so
you do not have to worry in your routes and filters about it.
Note that logging is only enabled for <tt>Sinatra::Application</tt> by
default, so if you inherit from <tt>Sinatra::Base</tt>, you probably want to
enable it yourself:
class MyApp < Sinatra::Base
configure(:production, :development) do
enable :logging
end
end
=== Mime Types
When using <tt>send_file</tt> or static files you may have mime types Sinatra

View file

@ -138,6 +138,11 @@ module Sinatra
request.session
end
# Access shared logger object.
def logger
request.logger
end
# Look up a media type by file extension in Rack's mime registry.
def mime_type(type)
Base.mime_type(type)
@ -1221,10 +1226,10 @@ module Sinatra
# an instance of this class as end point.
def build(*args, &bk)
builder = Rack::Builder.new
setup_logging builder
setup_sessions builder
builder.use Rack::CommonLogger if logging?
builder.use Rack::MethodOverride if method_override?
builder.use ShowExceptions if show_exceptions?
builder.use Rack::MethodOverride if method_override?
builder.use ShowExceptions if show_exceptions?
middleware.each { |c,a,b| builder.use(c, *a, &b) }
builder.run new!(*args, &bk)
builder
@ -1235,6 +1240,19 @@ module Sinatra
end
private
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
else
builder.use Rack::NullLogger
end
end
def setup_sessions(builder)
return unless sessions?
options = { :secret => session_secret }

View file

@ -861,6 +861,36 @@ class HelpersTest < Test::Unit::TestCase
end
end
describe 'logger' do
it 'logging works when logging is enabled' do
mock_app do
enable :logging
get '/' do
logger.info "Program started"
logger.warn "Nothing to do!"
end
end
io = StringIO.new
get '/', {}, 'rack.errors' => io
assert io.string.include?("INFO -- : Program started")
assert io.string.include?("WARN -- : Nothing to do")
end
it 'logging works when logging is disable, but no output is produced' do
mock_app do
disable :logging
get '/' do
logger.info "Program started"
logger.warn "Nothing to do!"
end
end
io = StringIO.new
get '/', {}, 'rack.errors' => io
assert !io.string.include?("INFO -- : Program started")
assert !io.string.include?("WARN -- : Nothing to do")
end
end
module ::HelperOne; def one; '1'; end; end
module ::HelperTwo; def two; '2'; end; end