Skip to content

Commit

Permalink
Merge branch 'better_logging'
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGES
  • Loading branch information
rkh committed Mar 21, 2011
2 parents d0557f5 + fe3cf84 commit 7d3b99e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions README.rdoc
Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions lib/sinatra/base.rb
Expand Up @@ -138,6 +138,11 @@ def session
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)
Expand Down Expand Up @@ -1221,10 +1226,10 @@ def new(*args, &bk)
# 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
Expand All @@ -1235,6 +1240,19 @@ def call(env)
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 }
Expand Down
30 changes: 30 additions & 0 deletions test/helpers_test.rb
Expand Up @@ -861,6 +861,36 @@ def send_file_app(opts={})
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

Expand Down

0 comments on commit 7d3b99e

Please sign in to comment.