How do you debug a Sinatra app like a Rails app?
Solution 1
You could try adding a before filter that prints out the parameters
before do
puts '[Params]'
p params
end
Solution 2
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 torack.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.
Solution 3
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.
Solution 4
I recommend using Pry or the Ruby debugger. With Pry, you can use the command line to traverse your methods and variables like you would in bash.
See "How to use Pry with Sinatra?".
Solution 5
In order to find the best way to debug sinatra app, I created a repo at github. The most useful part is step into method debug, which looks like below.
Here is the repo: https://github.com/hlee/sinatra_debugger_example
nova
Updated on July 29, 2020Comments
-
nova almost 4 years
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}"