2012-10-28 13 views
0

我有以下代碼,並且我想將其運行時生成的任何錯誤寫入日誌文件。如何在Rails 3.2中的日誌文件中記錄從模型方法生成的所有錯誤

我該如何做到這一點在鐵軌?

def get_score 
if status == "approved" 
     begin 
      metrics = Popularity.get_metrics(url,nil ,:resource) 

      update_column(:metrics, metrics.to_yaml) 
      update_column(:score,Popularity.score(metrics,:resource)) 
      reputation = reputations.build(:metrics => metrics) 
      reputation.score = score 
      reputation.save 

     rescue 
     #write the error that was raised to a log file 
     end 
    end 

回答

0

這個怎麼樣

def get_score 
    if status == "approved" 
     begin 
      metrics = Popularity.get_metrics(url,nil ,:resource) 

      update_column(:metrics, metrics.to_yaml) 
      update_column(:score,Popularity.score(metrics,:resource)) 
      reputation = reputations.build(:metrics => metrics) 
      reputation.score = score 
      reputation.save 

     rescue => e 
      Rails.logger.debug "Opps Error Occurred -- > #{e} " 
     end 
    end 

Rails.logger.debug將記錄在log/[your environment].log

誤差與debug標籤或者你也可以使用

Rails.logger.info "Opps Error Occurred -- > #{e} "

相關問題