【问题标题】:Rails 4 exception stack traces don't show in developmentRails 4 异常堆栈跟踪未在开发中显示
【发布时间】:2016-04-16 23:28:35
【问题描述】:

我正在关注rails tutorial book 并在学习教程时使用c9.io Web IDE。在处理我的示例应用程序时,我注意到在开发模式下页面上没有显示异常。此外,log/development.log 也没有记录异常堆栈跟踪。这种困境让我很难调试代码中的错误。

应用程序出错

development.log 中的错误

Completed 500 Internal Server Error in 116ms (ActiveRecord: 2.3ms)

我已经尝试/研究过的内容

  • config.log_level = :debug(在 config/environments/development.rb 中)
  • config.consider_all_requests_local = true
  • this
  • that
  • the other

config/environments/development.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
  config.log_level = :debug
end

请记住,我对试图为抛出的异常找到解决方案的答案感兴趣。我只是希望启用异常堆栈跟踪,以便我可以自己调试问题。

2017 年 2 月 25 日编辑

宝石文件

source 'https://rubygems.org'

gem 'rails',                   '4.2.2'
gem 'bcrypt',                  '3.1.7'
gem 'faker',                   '1.4.2'
gem 'carrierwave',             '0.10.0'
gem 'mini_magick',             '3.8.0'
gem 'fog',                     '1.36.0'
gem 'will_paginate',           '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass',          '3.2.0.0'
gem 'sass-rails',              '5.0.2'
gem 'uglifier',                '2.5.3'
gem 'coffee-rails',            '4.1.0'
gem 'jquery-rails',            '4.0.3'
gem 'turbolinks',              '2.3.0'
gem 'jbuilder',                '2.2.3'
gem 'sdoc',                    '0.4.0', group: :doc

group :development, :test do
  gem 'sqlite3',     '1.3.9'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

group :test do
  gem 'minitest-reporters', '1.0.5'
  gem 'mini_backtrace',     '0.1.3'
  gem 'guard-minitest',     '2.3.1'
end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
  gem 'puma',           '3.1.0'
end

【问题讨论】:

  • 您使用的是哪台服务器以及如何运行它?
  • 它在开发中使用 WEBrick。我正在使用rails s -b $IP -p $PORT 运行它(c9.io 托管环境中需要$IP$PORT。请参阅:link)。
  • 您找到解决方案了吗?我实际上遇到了同样的问题,不知道如何解决它
  • 此行为表明您可能正在生产模式下运行您的应用程序。你确定你在development env 中运行你的服务器吗? c9 IDE 中的输出选项卡应该是这样的:Rails 4.2.5 application starting in development on http://0.0.0.0:8080 - 你能确认一下吗?我刚刚在 c9 上成功设置了一个最小的 rails 应用程序,它显示异常正常 - 请参阅 so-36670968-borama.c9users.io 。另外,当您意识到未显示异常时,您是在 Rails 教程的哪一点?能否提供具体教程部分的链接?
  • 是的,开发模式。 $rails s => 引导 Puma => Rails 5.0.2 应用程序开始在 localhost:3000 上开发 => 运行 rails server -h 以获得更多启动选项 Puma 从单一模式开始... * 版本 3.8.2 (ruby 2.3.1- p112),代号:Sassy Salamander * 最小线程:5,最大线程:5 * 环境:开发 * 监听 tcp://localhost:3000

标签: ruby-on-rails ruby-on-rails-4 exception


【解决方案1】:

尝试更新您的网络控制台 gem。

bundle update web-console

【讨论】:

  • 这是我的问题,我有 2.x 版,更新到 3.x 后,一切都正常了
  • 在将 Rails 4 应用程序更新到 Rails 5 后也是我的问题。
  • 并确保它没有被锁定在 Gemfile ;)
  • 在 Rails 4 下也有同样的问题。我刚刚删除了 web-console。我从不使用它。然后显示堆栈跟踪页面。
【解决方案2】:

与此同时,我能够找到问题的根源,但仍然不是解决方案。

我正在使用rollbar-gem 扩展来发布生产中的错误,这实际上似乎吸收了开发中的所有错误日志(即使它已被手动禁用)。因此删除 gem 和所有代码后,我可以再次访问我的日志。

我在 gems 页面上发布了一个关于此的问题。

【讨论】:

  • 尝试将rollbar-gem 放入您的Gemfile 中的group :production 块中,然后它不会在开发时加载。
  • 是的,但您仍然必须确保仅在 production 上调用初始化代码,否则 rails 将拒绝启动
  • 也许只是围绕整个初始化程序的if Rails.env.production?...end?编辑:也许 if defined?(Rollbar)...end 包围整个初始化程序将是一个更好的做法,因为它会检查 Rollbar 是否已加载,因为它只会在生产中使用 Gemfile。
【解决方案3】:

您没有在development 中运行应用程序。 Rails 应用程序可以使用rails srails server 的缩写)在本地运行。确保您已将 RAILS_ENV 或等效设置为 development

【讨论】:

  • 我运行RAILS_ENV=development rails s 以确保服务器真正在开发模式下运行。结果还是一样
  • 你能确认它真的吗? Rails.env 等于什么?
  • 确实如此。当我在生产服务器上运行它时,它还会记录我需要的调试信息,包括堆栈跟踪。因此,即使它可以在生产环境中运行,它仍然应该记录所需的数据
  • 在 C9 上,执行 rails s 将产生以下输出:Rails 4.2.2 application starting in development on http://0.0.0.0:8080
【解决方案4】:

查看服务器上的生产日志 (log/production.log) 或其他日志的 log/ 目录,例如 log/unicorn.stderr.loglog/puma.log

【讨论】:

  • 它在生产服务器上按预期完美记录,但不是在开发中
【解决方案5】:

检查 'log/development.log' 文件夹和文件的权限,以确保 rails 可以在那里写入。

【讨论】:

    【解决方案6】:

    尽量让 config.consider_all_requests_local = true(在 config/environments/development.rb 中)

    并删除此行 config.log_level = :debug(在 config/environments/development.rb 中)

    另外,您可以尝试使用 gem 更好的错误来查看它是否可以解决问题 https://github.com/charliesome/better_errors

    如果这不能解决问题,通常运行“捆绑更新”,看看是否有导致问题的 gem

    【讨论】:

    • 不,两者都没有变化
    【解决方案7】:

    将此添加到您的 application.html.erb 文件中。它应该为您提供更多详细信息

    <%= debug(params)%>
    

    【讨论】:

    • 获取更多详细信息可能会提供答案,但这应该是对问题的评论。
    【解决方案8】:

    在我的例子中,只设置 config.consider_all_requests_local = true 确实使 puma 显示所有错误和完整的回溯。

    【讨论】:

      【解决方案9】:

      我建议使用另一种方式来显示错误,而不是使用默认的 Rails。

      我发现了同样的问题并尝试了所有方法,但 gem better_errors 挽救了这一天。

      您可以在 Gemfile 中使用

      group :development, :test do # I use that only in dev
        gem "better_errors"
        # dependency
        gem "binding_of_caller"
      end
      

      【讨论】:

      • 值得注意的是,我遇到了与 Rails 5 上的 OP 相同的问题,这是由 binding_of_caller 的过时版本引起的。升级为我解决了问题。
      【解决方案10】:

      我在迁移到 Rails 5 应用的 Rails 4 应用中遇到了同样的错误。

      在开发模式和config.consider_all_requests_local = true 上运行 WEBrick。 结果是 gem 'web-console';如果 Gemfile 是特定版本,则使用 'bundle update web-console' 不起作用。所以我必须手动更改并更新。

      【讨论】:

        【解决方案11】:

        这个问题通过添加解决了我

        # encoding: utf-8
        

        给我的控制器。在this Rails issue 中找到解决方案。

        【讨论】:

          猜你喜欢
          • 2017-08-09
          • 2010-10-16
          • 1970-01-01
          • 2014-01-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多