In my main Sinatra controller, I want to debug the params hash after it is POSTed from a form.
I have added:
puts params.inspect
and
set :logging, :true
The params.inspect
works if everything goes well. But if an error happens before the controller is executed I'm not getting any information about the error like I would in Rails by default.
What's the best way to get useful debug info?
This example did not work at all (the app wouldn't even start after I added this code):
configure do
Log = Logger.new("sinatra.log")
Log.level = Logger::INFO
end
followed by:
Log.info "#{@users.inspect}"
You could try adding a before filter that prints out the parameters
before do puts '[Params]' p params end
My opinion is that for debug you should use configure :development
do because some debugging flags are turned on in this scenario. So, under your configure do
block you can enable the flags:
enable :logging, :dump_errors, :raise_errors
and for your logging facility:
log = File.new("sinatra.log", "a") STDOUT.reopen(log) STDERR.reopen(log)
From the Sinatra handbook:
dump_errors
option controls whether the backtrace is dumped to rack.errors
when an exception is raised from a route. The option is enabled by default for top-level apps.
raise_errors
- allow exceptions to propagate outside of the app (...) The :raise_errors
option is disabled by default for classic style apps and enabled by default for Sinatra::Base subclasses.
I also use the
puts "something" + myvar.inspect
method in the middle of my controllers.
As @jshen says, ruby-debug is a very nice tool - if you are using Ruby 1.8.
In order to use it in Sinatra, insert
require 'ruby-debug/debugger'
where you'd like debugging to start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With