bash Development.log 日志文件未记录 Rails SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7432166/
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-18 00:47:29  来源:igfitidea点击:

Development.log log file isn't logging Rails SQL queries

ruby-on-railsrubybashtailrailstutorial.org

提问by Andrew Lauer Barinov

I am following Michael Hartl's Rails Tutorial Here:

我正在关注 Michael Hartl 的 Rails 教程:

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

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

I use this command to track the SQL queries in a separate window:

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

tail -f log/development.log

However while I am in the sandboxed rails console, the log is not updated with SQL statements, instead they show up within the rails console. How can I correct this behavior?

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

I should add that my database migrations and changes to the data model (new tables, etc) ARE reflected in the log. Only the SQL statements propagated by methods inside the rails console are omitted (and are displayed in the rails console instead).

我应该补充一点,我的数据库迁移和对数据模型(新表等)的更改都反映在日志中。只有通过 rails 控制台内的方法传播的 SQL 语句被省略(而是显示在 rails 控制台中)。

Here is my Gemfile:

这是我的 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'

Here is the output of the rails console:

这是 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 => "[email protected]")
   (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", "[email protected]"], ["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: "[email protected]", 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: "[email protected]", created_at: "2011-09-15 20:34:09", updated_at: "2011-09-15 20:34:09"> 
ruby-1.9.2-p290 :003 > 

And here are the settings in my config/environments/development.rbfile

这是我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

Finally here is the development.logoutput so far:

最后,这是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')

采纳答案by KenB

Apparently showing the SQL in the console rather than development.log is default behavior for Rails 3.1. I haven't found a configuration option for changing that behavior, but I have found that, once the console is running, you can just do:

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

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

and that will take the sql out of the console and put it back in development.log. Or, if you don't want to do that every time you fire up the console, you can edit gems/railties-(version)/lib/rails/console.rb, and make the above assignment after the line in the start method that reads:

这将从控制台中取出 sql 并将其放回 development.log。或者,如果您不想每次启动控制台时都这样做,您可以编辑 gems/railties-(version)/lib/rails/console.rb,并在 start 方法中的行之后进行上述分配内容如下:

@app.load_console

Not saying it's a good solution, but it'll tide me over until I find something better...

并不是说这是一个很好的解决方案,但它会让我渡过难关,直到我找到更好的方法......

回答by Cody Caughlan

Ensure that your log levelis set to :debugin config/environments/development.rb, like so:

确保您的日志级别设置为:debugin config/environments/development.rb,如下所示:

 config.log_level = :debug

回答by prusswan

Combining KenB's answer and how can i load a file in ruby on rails console?,

结合 KenB 的回答以及如何在 Rails 控制台上的 ruby​​ 中加载文件?,

simply create a .irbrc file and include the following line:

只需创建一个 .irbrc 文件并包含以下行:

ActiveRecord::Base.logger = Rails.logger

回答by prusswan

There's also some info on the old Rails wiki about using custom loggers and setting up the log levels for that: http://oldwiki.rubyonrails.org/rails/pages/HowtoConfigureLogging

在旧的 Rails wiki 上还有一些关于使用自定义记录器并为此设置日志级别的信息:http: //oldwiki.rubyonrails.org/rails/pages/HowtoConfigureLogging

For the standard Rails logger you can use the following line in config/environments/development.rb:

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

config.log_level = :debug

For custom loggers (such as log4j), you will need to use:

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

config.logger.level = Logger::DEBUG

or whatever the logger accepts.

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

回答by Artem Kalinchuk

Remove config.active_record.logger = nilfrom application.rb...if it's set.

config.active_record.logger = nilapplication.rb...中删除,如果已设置。