开发日志文件没有记录Rails SQL查询

11
我正在按照Michael Hartl的Rails教程进行学习,链接在此:http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-one#top。我使用以下命令在一个单独的窗口中跟踪SQL查询:
tail -f log/development.log

然而,当我在受沙盒限制的Rails控制台中时,日志没有更新SQL语句,而是显示在Rails控制台中。我该如何更正这种行为?

我应该补充说明,我的数据库迁移和数据模型更改(新表等)在日志中得到了反映。只有由Rails控制台内部方法传播的SQL语句被省略了(并且显示在Rails控制台中)。

这是我的Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.1.0'

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

group :development do
  gem 'rspec-rails', '2.6.1'
  gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
end

group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.3'
  gem 'spork', '0.9.0.rc8'
  gem 'guard-spork'
  gem 'autotest', '4.4.6'
  gem 'autotest-rails-pure', '4.1.2'
  gem 'autotest-fsevent', '0.2.4'
  gem 'autotest-growl', '0.2.9'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

gem 'jquery-rails'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

这里是 rails 控制台的输出:
Larson-2:sample larson$ rails console --sandbox
Loading development environment in sandbox (Rails 3.1.0)
Any modifications you make will be rolled back on exit
ruby-1.9.2-p290 :001 > user = User.create(:name => "A Nother", :email => "another@example.org")
   (0.1ms)  SAVEPOINT active_record_1
  SQL (13.4ms)  INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Thu, 15 Sep 2011 20:34:09 UTC +00:00], ["email", "another@example.org"], ["name", "A Nother"], ["updated_at", Thu, 15 Sep 2011 20:34:09 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
 => #<User id: 1, name: "A Nother", email: "another@example.org", created_at: "2011-09-15 20:34:09", updated_at: "2011-09-15 20:34:09"> 
ruby-1.9.2-p290 :002 > user.destroy
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.3ms)  DELETE FROM "users" WHERE "users"."id" = ?  [["id", 1]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
 => #<User id: 1, name: "A Nother", email: "another@example.org", created_at: "2011-09-15 20:34:09", updated_at: "2011-09-15 20:34:09"> 
ruby-1.9.2-p290 :003 > 

以下是我 config/environments/development.rb 文件中的设置:

Sample::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

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # 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

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true

    #Ensure that log level is set to capture ALL messages (from Stack Overflow)
    config.log_level = :debug

end

最后这是 development.log 的输出结果:

Larson-2:sample larson$ tail -f log/development.log       
   (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" 
   (0.0ms)  PRAGMA index_list("users")
   (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" 
   (0.2ms)  select sqlite_version(*)
   (1.8ms)  CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255), "created_at" datetime, "updated_at" datetime) 
   (1.1ms)  CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
   (0.0ms)  PRAGMA index_list("schema_migrations")
   (1.6ms)  CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
   (0.1ms)  SELECT version FROM "schema_migrations"
   (1.0ms)  INSERT INTO "schema_migrations" (version) VALUES ('20110915130358')

我遇到了与几乎相同设置相同的问题。 我也想知道如何修复。 - Fred Fickleberry III
同样的情况在这里,希望能够解决... - KenB
这里情况也一样。 - JonatasTeixeira
5个回答

15

请确保在config/environments/development.rb文件中,将日志级别设置为:debug

 config.log_level = :debug

3
我按照你的建议去做了,但问题还是一样。请看上面我的控制台输出(包括tail和rails console),以及我的config/environments/development.rb文件。 - Andrew Lauer Barinov

4

显然,在Rails 3.1中,将SQL显示在控制台而不是development.log中是默认行为。我没有找到更改该行为的配置选项,但我发现,一旦控制台运行,您只需执行以下操作即可:

irb(main):001:0> ActiveRecord::Base.logger = Rails.logger

这将把SQL从控制台中移除并放回到development.log文件中。或者,如果您不想每次启动控制台时都执行此操作,您可以编辑gems/railties-(version)/lib/rails/console.rb,并在start方法中读取上述赋值语句后进行修改:

@app.load_console

不是说这是一个好的解决方案,但它将让我度过难关,直到我找到更好的东西...


1

关于使用自定义记录器和设置其日志级别,旧版Rails维基上也提供了一些信息:http://oldwiki.rubyonrails.org/rails/pages/HowtoConfigureLogging

对于标准的Rails记录器,您可以在 config/environments/development.rb 中使用以下代码:

config.log_level = :debug

对于自定义日志记录器(例如log4j),您需要使用:

config.logger.level = Logger::DEBUG

或者任何日志记录器接受的内容。


1

0
application.rb中删除config.active_record.logger = nil...如果它被设置了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接