2011-09-15 38 views
10

我關注的邁克爾·哈特爾的Rails的教程在這裏:Development.log日誌文件不記錄Rails的SQL查詢

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

我使用這個命令來追蹤在一個單獨的窗口中的SQL查詢:

tail -f log/development.log 

但是,當我在沙盒軌道控制檯中時,日誌不會使用SQL語句更新,而是會在軌道控制檯中顯示。我怎樣才能糾正這種行爲?

我應該補充一點,我的數據庫遷移和對數據模型(新表格等)的更改都反映在日誌中。只有通過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' 

這裏是軌道控制檯輸出:

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 > 

這裏是我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') 
+0

我遇到了幾乎相同的設置相同的東西。還想知道修復。 –

+0

同樣的情況在這裏,會喜歡修復... – KenB

+0

和這裏相同的情況.. – JonatasTeixeira

回答

3

顯然在控制檯而不是development.log中顯示SQL是Rails 3.1的默認行爲。我還沒有找到改變這種行爲的配置選項,但我發現,一旦控制檯正在運行,你可以做:

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

,這將需要在SQL出去控制檯,並把它回到development.log。或者,如果每次啓動控制檯時都不想這樣做,則可以編輯gems/railties-(version)/lib/rails/console.rb,並在啓動方法中的行之後進行上述分配上面寫着:

@app.load_console 

不是說這是一個很好的解決方案,但它會潮我過去,直到我找到更好的......

13

確保您log levelconfig/environments/development.rb設置爲:debug,像這樣:

config.log_level = :debug 
+2

我遵循了你的建議,但我仍然有同樣的問題。看到我的控制檯輸出(對於尾部和軌道控制檯)以及我的config/environments/development.rb文件。 –

0

application.rb刪除config.active_record.logger = nil ...如果它的設置。

1

還有對舊Rails的維基一些信息,有關使用自定義記錄程序和設置該日誌級別:http://oldwiki.rubyonrails.org/rails/pages/HowtoConfigureLogging

對於標準的Rails記錄器可以使用在config/environments/development.rb以下行:

config.log_level = :debug 

對於自定義記錄程序(如log4j的),你將需要使用:

config.logger.level = Logger::DEBUG 

或任何記錄器接受。

相關問題