2012-09-19 39 views
1

所以,一個簡單的小問題。每次我在我的「獲取」或「後」塊之一,DataMapper的執行一些交易的時候,我得到的輸出看起來像這樣...如何在Sinatra中沉默DataMapper

core.local - - [19/Sep/2012:09:04:54 CEST] "GET /eval_workpiece/state?id=24 HTTP/1.1" 200 4 
- -> /eval_workpiece/state?id=24 

這是我喜歡有點太冗長。我可以關閉此反饋嗎?

回答

1

要切斷所有的日誌:

DataMapper.logger = nil 

要改變冗長:

DataMapper.logger.set_log(logger, :warn) # where logger is Sinatra's logging object 

其他水平:fatal => 7, :error => 6, :warn => 4, :info => 3, :debug => 0http://rubydoc.info/gems/dm-core/1.1.0/DataMapper/Logger

2

這不是DataMapper的記錄,這是記錄由WEBrick服務器完成,其中logs all requests using these two formats by default

(請注意,這個不是機架日誌記錄,但Rack :: CommonLogger使用相同(或至少非常相似)的格式)。

最簡單的方法就是切換到另一臺不添加自己的日誌記錄的服務器,例如Thin。

如果你想繼續使用WEBrick,你需要找到一種方法從你的Sinatra應用程序傳遞選項。目前發佈的Sinatra寶石(1.3.3)不允許簡單的方法來做到這一點,但current master allows you to set the :server_options setting which Sinatra will then pass on。所以在將來你應該可以做到這一點:

set :server_settings, {:AccessLog => []} 

爲了沉默WEBrick。

暫時可以這樣講一下你的應用程序文件的末尾(我假設你推出的東西,如ruby my_app_file.rb您的應用程序):

disable :run 
Sinatra::Application.run! do |server| 
    server.config[:AccessLog] = [] 
end 
0

如果你正在運行用的ActiveSupport,您可以使用內核擴展:

quietly { perform_a_noisy_task } 

這暫時結合STDOUT和SDTERR到/ dev/null作爲塊的持續時間。既然你可能不希望打壓所有輸出,理論上你可以這樣做:

with_warnings(:warn) { # or :fatal or :error or :info or :debug 
    perform_a_noisy_task 
} 

和適當的信息會被壓抑。 (注意:我說'理論上',因爲在我的Padrino/DataMapper環境中使用with_warnings給了我一個看似無關的錯誤:YMMV。)