ruby-on-rails - Development.log 日志文件不记录 Rails SQL 查询

标签 ruby-on-rails ruby bash tail railstutorial.org

我在这里关注 Michael Hartl 的 Rails 教程:

http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-one#top

我使用此命令在单独的窗口中跟踪 SQL 查询:

tail -f log/development.log

然而,当我在沙盒 Rails 控制台中时,日志不会用 SQL 语句更新,而是显示在 Rails 控制台中。我该如何纠正这种行为?

我应该补充一点,我的数据库迁移和对数据模型(新表等)的更改都反射(reflect)在日志中。仅省略由 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')

最佳答案

确保您的 log levelconfig/environments/development.rb 中设置为 :debug,如下所示:

 config.log_level = :debug

关于ruby-on-rails - Development.log 日志文件不记录 Rails SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7432166/

相关文章:

bash - 使用 `read` 读取密码时如何回显星号 (*)?

regex - 需要将存储在 bash 变量中的 IP 地址分解为八位字节

ruby-on-rails - 具有多对多关联的模型的计数器缓存

javascript - 如何将字符串变量传递给 Morris.js 中的 labels 选项

ruby-on-rails - 如何在 Rails 的 json_response 中显示所有关联项

Ruby 编程控制和元组合

ruby-on-rails - 如何使用 Ruby 比较两个表

bash - 在 bash 变量的开头、结尾和逗号周围添加双引号

ruby-on-rails - Rails 4 + bootstrap 设置 Assets

ruby-on-rails - Rbenv 没有使用正确的版本