Ruby-on-rails 您如何像 Rails 应用程序一样调试 Sinatra 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1364755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:41:53  来源:igfitidea点击:

How do you debug a Sinatra app like a Rails app?

ruby-on-railsrubydebuggingloggingsinatra

提问by nova

In my main Sinatra controller, I want to debug the params hash after it is POSTed from a form.

在我的主要 Sinatra 控制器中,我想在从表单发布后调试 params 哈希。

I have added:

我已经添加了:

puts params.inspect

and

set :logging, :true

The params.inspectworks 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.

params.inspect如果一切顺利的工作。但是,如果在执行控制器之前发生错误,我不会像在 Rails 中默认那样获得有关错误的任何信息。

What's the best way to get useful debug info?

获得有用调试信息的最佳方式是什么?

This exampledid 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}"

采纳答案by BaroqueBobcat

You could try adding a before filter that prints out the parameters

您可以尝试添加一个打印出参数的 before 过滤器

before do
  puts '[Params]'
  p params
end

回答by include

My opinion is that for debug you should use configure :developmentdo because some debugging flags are turned on in this scenario. So, under your configure doblock you can enable the flags:

我的意见是,对于调试,您应该使用 configure :developmentdo,因为在这种情况下会打开一些调试标志。因此,在您的configure do块下,您可以启用标志:

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:

来自 Sinatra 手册:

  • dump_errorsoption controls whether the backtrace is dumped to rack.errorswhen 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_errorsoption is disabled by default for classic style apps and enabled by default for Sinatra::Base subclasses.

  • dump_errors选项控制rack.errors当路由引发异常时是否将回溯转储到。该选项默认为顶级应用启用。

  • raise_errors- 允许异常在应用程序之外传播 (...) 该:raise_errors选项默认为经典样式应用程序禁用,默认为 Sinatra::Base 子类启用。

I also use the

我也用

puts "something" + myvar.inspect

method in the middle of my controllers.

方法在我的控制器中间。

回答by asymmetric

As @jshen says, ruby-debug is a very nice tool - if you are using Ruby 1.8.

正如@jshen 所说,ruby-debug 是一个非常好的工具 - 如果您使用的是 Ruby 1.8。

In order to use it in Sinatra, insert

为了在 Sinatra 中使用它,插入

require 'ruby-debug/debugger' 

where you'd like debugging to start.

您希望从哪里开始调试。

回答by Ryan

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.

我建议使用 Pry 或 Ruby 调试器。使用 Pry,您可以像在 bash 中一样使用命令行来遍历您的方法和变量。

See "How to use Pry with Sinatra?".

请参阅“如何将 Pry 与 Sinatra 一起使用?”。

回答by Race

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.

为了找到调试 sinatra 应用程序的最佳方式,我在 github 创建了一个 repo。最有用的部分是进入方法调试,如下所示。

enter image description here

在此处输入图片说明

Here is the repo: https://github.com/hlee/sinatra_debugger_example

这是回购:https: //github.com/hlee/sinatra_debugger_example

回答by jshen

I'd highly recommend using ruby-debug for Ruby 1.8. It's very easy to setup and use once you learn the four or five basic commands. It will allow you to set a breakpoint where you want to inspect the params and interactively play with it like you would in IRB.

我强烈建议在 Ruby 1.8 中使用 ruby​​-debug。一旦您学会了四个或五个基本命令,它就非常容易设置和使用。它将允许您在要检查参数的位置设置断点,并像在 IRB 中一样交互地使用它。

回答by Zack Xu

I'd recommend using the debuggergem for Ruby 1.9.

我建议使用Ruby 1.9的调试器gem。

To use this with your Sinatra app:

要将它与您的 Sinatra 应用程序一起使用:

  1. Add the gem to your Gemfile

    gem "debugger"
    
  2. Install the gem by running

    bundle
    
  3. In your main sinatra app file, add

    require 'debugger'
    
  4. Now to debug, you just have to add the following line

    debugger
    

    anywhere in your code.

  1. 将 gem 添加到您的 Gemfile 中

    gem "debugger"
    
  2. 通过运行安装 gem

    bundle
    
  3. 在您的主要 sinatra 应用程序文件中,添加

    require 'debugger'
    
  4. 现在要调试,您只需添加以下行

    debugger
    

    在您的代码中的任何地方。

When the code is being executed and debuggeris seen, code would stop and you can inspect variables, run commands (using eval), etc.

当代码正在执行并被debugger看到时,代码将停止,您可以检查变量、运行命令(使用eval)等。

回答by notapatch

Ruby Debuggers in Sinatra

Sinatra 中的 Ruby 调试器

You are free to use Ruby debuggers for Sinatra. The main issue is that you will need to know which Ruby Gem is used for which Ruby version. For most people now that means Ruby 2.X and byebug.

您可以自由地为 Sinatra 使用 Ruby 调试器。主要问题是您需要知道哪个 Ruby Gem 用于哪个 Ruby 版本。对于现在大多数人来说,这意味着 Ruby 2.X 和byebug

Ruby Debuggers by Ruby version

Ruby 版本的 Ruby 调试器

 Ruby Version   Gem Install      require                add breakpoint
 ---------------------------------------------------------------------
 1.8.X          ruby-debug      'ruby-debug/debugger'   debugger
 1.9.X          debugger        'debugger'              debugger
 2.0.0+         byebug          'byebug'                byebug

Once you know that you must install the gem, require it in your code and then add breakpoints by typing a keyword. Taking byebug as an example:

一旦你知道你必须安装 gem,在你的代码中 require 它,然后通过输入关键字来添加断点。以byebug为例:

  1. install: gem install byebug
  2. require: require 'byebug'
  3. breakpoint: byebug
  1. 安装: gem install byebug
  2. 要求: require 'byebug'
  3. 断点: byebug

Further Reading

进一步阅读

Byebug project

再见项目

Byebug guide to use a terminal debugger

Byebug 使用终端调试器的指南

回答by Zach Inglis

回答by Emily

I'm guessing since you mentioned your main Sinatra controller, you have more than one, which means you're subclassing Sinatra::Base rather than using a classic (top-level) Sinatra app. If this is indeed the case, a lot of the default error-handling that Sinatra does is disabled by default.

我猜既然你提到了你的主要 Sinatra 控制器,你有不止一个,这意味着你正在继承 Sinatra::Base 而不是使用经典的(顶级)Sinatra 应用程序。如果确实如此,那么 Sinatra 所做的许多默认错误处理在默认情况下都是禁用的。

If you include set :show_exceptions, true if development?in the class definition, you'll get friendly error pages that include a stack trace, params, etc, instead of just an internal server error. Another option is to set :raise_errors, falseand then include a error do ... endblock that will run whenever one of your routes raises an error.

如果您包含set :show_exceptions, true if development?在类定义中,您将获得友好的错误页面,其中包括堆栈跟踪、参数等,而不仅仅是内部服务器错误。另一种选择是set :raise_errors, false然后包含一个error do ... end块,该块将在您的一个路由出现错误时运行。