簡體   English   中英

禁用 Rails 3.2.1 控制台 SQL 日志記錄

[英]Disable Rails 3.2.1 Console SQL Logging

我需要在我的開發數據庫中更新一堆記錄 (200k),但是通過 rails 控制台運行查詢導致我的計算機窒息(部分原因是控制台打印出查詢的結果)。

例子:

1.9.3p194 :083 > Player.first
 => #<Player id: 1, IsActive: false, IsVisible: false, FirstName: "Ovie", LastName: "Soko", HeightFeet: 6, HeightInches: 8, Weight: 210, Birthday: nil, Gender: "M", HometownCity: "London", HometownState: "", HometownZip: "", HometownCountry: "", HighSchoolId: "", HighSchoolIdTemp: 0, HighSchoolGradYear: "2009", CollegeYear: "", Redshirted: false, Transferred: false, CollegeId: "{299F909C-88D9-4D26-8ADC-3EC1A66168BB}", CollegeIdTemp: 844, CollegeGradYear: "2013", OtherAccountId: 0, PreviousCollegeId: 0, CurrentTeamId: 0, LateralRecommendationReason: "", LateralRecommendationLink: "", CreationDate: "2011-02-16 20:53:34.877000000", CreatedBy: "", LastModifiedDate: "2011-02-16 20:53:34.877000000", LastModifiedBy: "", TwitterLink: "", FacebookLink: "", PersonalWebsite: "", PlayerImage: "", FirstNameNickName: "", NeulionID: 0, OtherTeamID: 0, OtherSportTypeID: 0, SourceDataTypeID: 1, PlayerTypeID: 2, LoadID: "", SameNameTeammate: "", SameNameSchoolMate: "", SD_SportID: 0, SD_PlayerID: 0, ZeroNCAAStats: "", ModifiedByPythonGame: "", Missing2011: "", Transfer2011: "", RecruitingClass: "\r", created_at: nil, updated_at: nil, high_school_id: nil, high_school_name: nil, not_valid: nil, city_to_zip_id: nil, user_id: nil> 

正如我們所看到的,打印 20 萬條記錄將不是 bueno。

我在這里嘗試了解決方案: Disable Rails SQL logging in console ,但在那之后我仍然得到日志輸出。

1.9.3p194 :087 > old_logger = ActiveRecord::Base.logger
 => #<Logger:0x007feb04b8de58 @progname=nil, @level=1, @default_formatter=#<Logger::Formatter:0x007feb04b8dde0 @datetime_format=nil>, @formatter=#<Logger::SimpleFormatter:0x007feb04b8dc50 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007feb04b8dcf0 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007feb04b8dcc8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007feb04b8dc78>>>> 
1.9.3p194 :088 > ActiveRecord::Base.logger = nil
 => nil 
1.9.3p194 :089 > Player.first
 => #<Player id: 1, IsActive: false, IsVisible: false, FirstName: "Ovie", LastName: "Soko", HeightFeet: 6, HeightInches: 8, Weight: 210, Birthday: nil, Gender: "M", HometownCity: "London", HometownState: "", HometownZip: "", HometownCountry: "", HighSchoolId: "", HighSchoolIdTemp: 0, HighSchoolGradYear: "2009", CollegeYear: "", Redshirted: false, Transferred: false, CollegeId: "{299F909C-88D9-4D26-8ADC-3EC1A66168BB}", CollegeIdTemp: 844, CollegeGradYear: "2013", OtherAccountId: 0, PreviousCollegeId: 0, CurrentTeamId: 0, LateralRecommendationReason: "", LateralRecommendationLink: "", CreationDate: "2011-02-16 20:53:34.877000000", CreatedBy: "", LastModifiedDate: "2011-02-16 20:53:34.877000000", LastModifiedBy: "", TwitterLink: "", FacebookLink: "", PersonalWebsite: "", PlayerImage: "", FirstNameNickName: "", NeulionID: 0, OtherTeamID: 0, OtherSportTypeID: 0, SourceDataTypeID: 1, PlayerTypeID: 2, LoadID: "", SameNameTeammate: "", SameNameSchoolMate: "", SD_SportID: 0, SD_PlayerID: 0, ZeroNCAAStats: "", ModifiedByPythonGame: "", Missing2011: "", Transfer2011: "", RecruitingClass: "\r", created_at: nil, updated_at: nil, high_school_id: nil, high_school_name: nil, not_valid: nil, city_to_zip_id: nil, user_id: nil> 

Rails 3.2.1 是否有新的 hack 或者我做錯了什么?

您不必讓控制台打印最后一條語句的結果。 這是一個人為的例子,但你可以讓你的最后一條語句為零:

irb(main):156:0> Player.update_all(:IsActive => true); nil

SQL (1.8ms)  UPDATE "players" SET "IsActive" = 't'
=> nil

我不認為你做錯了什么。 Player.all是否Player.all返回所有內容? 我的猜測是您看到的輸出可能是 Rails 主記錄器而不是 ActiveRecord 記錄器。

您可以在development.rb中將其中任何一個設置為 nil 。

編輯:

config.logger
config.active_record.logger
config.action_controller.logger
config.action_view.logger
config.action_mailer.logger
config.active_resource.logger

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM