New to sinatra: how to debug e.g. post parameters?

4,569 views
Skip to first unread message

Markus

unread,
Sep 13, 2010, 9:49:06 PM9/13/10
to sinatrarb
Hi,

I'm new to ruby and sinatrarb. My sinatra code looks very simple:

$ cat test.rb
require 'sinatra'
require 'sinatra/reloader' if development?

get '/' do
content_type :json
"0"
end

I start this simply with "ruby test.rb"

== Sinatra/1.0 has taken the stage on 4567 for development with backup
from WEBrick
[2010-09-14 03:44:37] INFO WEBrick 1.3.1
[2010-09-14 03:44:37] INFO ruby 1.9.1 (2010-01-10) [i686-linux]
[2010-09-14 03:44:37] INFO WEBrick::HTTPServer#start: pid=6194
port=4567

Now I can simple return the content of e.g. params with

get '/' do
content_type :json
"#{params}"
end

But I don't want to send the content back to client, but would like to
see it right in the console where I started sinatra (not in a separate
file).

I've searched and found http://codex.heroku.com/past/2010/3/1/logging/
, http://stackoverflow.com/questions/1364755/how-do-you-debug-a-sinatra-app-like-a-rails-app
and http://www.nickhammond.com/2009/03/28/easy-logging-with-sinatra/ ,
but I can't figure out how to apply this (I don't have a config file
and for the time being would like to simply use everything in one
file).

thanks for any pointers,
- Markus

Mika Tuupola

unread,
Sep 14, 2010, 7:34:07 AM9/14/10
to sina...@googlegroups.com

On Sep 14, 2010, at 4:49 AM, Markus wrote:

> But I don't want to send the content back to client, but would like to
> see it right in the console where I started sinatra (not in a separate
> file).


I usually do just something like

pp params

--
Mika Tuupola
http://www.appelsiini.net/

Markus

unread,
Sep 14, 2010, 12:31:46 PM9/14/10
to sinatrarb
Hi,

On Sep 14, 1:34 pm, Mika Tuupola <tuup...@appelsiini.net> wrote:
> On Sep 14, 2010, at 4:49 AM, Markus wrote:
>
> > But I don't want to send the content back to client, but would like to
> > see it right in the console where I started sinatra (not in a separate
> > file).
>
> I usually do just something like
>
> pp params

Wow, thanks for making me feel stupid :) Here's the confirmation I'm
new to ruby, I didn't realize that the output sent to the client is
the return value and I simply can use STDOUT (puts, p, pp) to see them
in the starting console.

Thanks so much,
- Markus

Michael Breen

unread,
Sep 14, 2010, 12:49:17 PM9/14/10
to sina...@googlegroups.com
You can also use the ruby-debug gem. After installing the gem just put `require ruby-debug/debugger` where you'd put the pp.

get '/' do
content_type :json

require ruby-debug/debugger
"#{params}"
end

Here's a nice intro post to ruby-debug: http://pivotallabs.com/users/chad/blog/articles/366-ruby-debug-in-30-seconds-we-don-t-need-no-stinkin-gui-

Best.
Mike

> --
> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
>

Lyndon Maydwell

unread,
Sep 14, 2010, 1:08:10 PM9/14/10
to sina...@googlegroups.com
Seconding ruby-debug. This is really invaluable when unit-tests just
don't cut it.

Markus Prinz

unread,
Sep 14, 2010, 1:41:50 PM9/14/10
to sina...@googlegroups.com

Keep in mind though that this depends on how you're hosting your app, and doesn't always work (e.g. with Passenger and Apache/Nginx).

Others have already mentioned ruby-debug, but I would also recommend some kind of logging system. There's quite a few gems out there that do this for you, but I tend to stick to the `Logger' class that comes with the standard library: http://rubydoc.info/docs/ruby-stdlib/1.9.2/Logger

Here's a simple program that demos its usage in a Sinatra program that should get you started:

require 'sinatra'
require 'logger'

configure do
# Create the log file if it doesn't exist,
# otherwise just start appending to it,
# preserving the previous content
log_file = File.open('my_app.log', 'a+')
# Don't buffer writes to this file. Recommended for development.
log_file.sync = true

logger = Logger.new(log_file)
# Log everything to the log file
logger.level = Logger::DEBUG

set :logger, logger
end

# Convenience method
def logger; settings.logger; end

get '/' do
logger.debug "params: #{params.inspect}"
"Hello, world!"
end

You can then do a `tail -f my_app.log` and see the logging output.

g, Markus

Reply all
Reply to author
Forward
0 new messages