2011-09-15 22 views
10

tôi sau Michael Hartl của Rails Tutorial đây:Development.log log file không được đăng nhập Rails SQL truy vấn

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

tôi sử dụng lệnh này để theo dõi các truy vấn SQL trong một cửa sổ riêng biệt:

tail -f log/development.log 

Tuy nhiên trong khi tôi đang ở trong bảng điều khiển đường ray hộp cát, nhật ký không được cập nhật với các câu lệnh SQL, thay vào đó chúng hiển thị trong bảng điều khiển đường ray. Làm cách nào để khắc phục hành vi này?

Tôi nên thêm rằng việc di chuyển và thay đổi cơ sở dữ liệu của tôi sang mô hình dữ liệu (bảng mới, v.v.) được phản ánh trong nhật ký. Chỉ các câu lệnh SQL được truyền bởi các phương thức bên trong bảng điều khiển đường ray được bỏ qua (và được hiển thị trong bảng điều khiển đường ray thay thế).

Đây là Gemfile tôi:

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' 

Đây là kết quả của đường ray console:

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 > 

Và đây là các thiết lập trong file config/environments/development.rb tôi

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 

Cuối cùng đây là đầu ra development.log cho đến thời điểm này:

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

Tôi đang trải nghiệm những điều tương tự với thiết lập tương tự. Cũng muốn biết bản sửa lỗi. –

+0

Tình huống tương tự ở đây, rất thích sửa chữa ... – KenB

+0

Và tình huống tương tự ở đây .. – JonatasTeixeira

Trả lời

3

Hiển thị SQL trong bảng điều khiển thay vì development.log là hành vi mặc định cho Rails 3.1. Tôi đã không tìm thấy một tùy chọn cấu hình cho việc thay đổi hành vi đó, nhưng tôi đã phát hiện ra rằng, một khi giao diện điều khiển đang chạy, bạn chỉ có thể làm:

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

và điều đó sẽ lấy sql ra khỏi giao diện điều khiển và đặt nó trở lại trong development.log. Hoặc, nếu bạn không muốn làm điều đó mỗi khi bạn khởi động bàn điều khiển, bạn có thể chỉnh sửa gem/railties- (phiên bản) /lib/rails/console.rb và thực hiện phép gán ở trên sau dòng trong phương thức khởi động mà đọc:

@app.load_console 

Không nói đó là một giải pháp tốt, nhưng nó sẽ triều tôi qua cho đến khi tôi tìm thấy một cái gì đó tốt hơn ...

13

Đảm bảo rằng log level của bạn được thiết lập để :debug trong config/environments/development.rb, như vậy:

config.log_level = :debug 
+2

Tôi đã làm theo đề xuất của bạn, nhưng tôi vẫn gặp sự cố tương tự. Xem đầu ra giao diện điều khiển của tôi (cho cả đuôi và bảng điều khiển đường ray) ở trên cũng như tệp config/environment/development.rb của tôi. –

0

Di config.active_record.logger = nil từ application.rb ... nếu nó được đặt.

1

Ngoài ra còn có một số thông tin trên wiki Rails cũ về việc sử dụng logger tùy chỉnh và thiết lập các mức log cho rằng: http://oldwiki.rubyonrails.org/rails/pages/HowtoConfigureLogging

Đối với Rails chuẩn Logger bạn có thể sử dụng dòng sau trong config/environments/development.rb:

config.log_level = :debug 

Đối logger tùy chỉnh (như log4j), bạn sẽ cần phải sử dụng:

config.logger.level = Logger::DEBUG 

hoặc bất kỳ trình ghi nhật ký nào chấp nhận.

Các vấn đề liên quan