0

I have the following code and I want to write any errors generated when running it to a log file.

How can I do that in rails?

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
4

1 回答 1

0

How about this

 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

The Rails.logger.debug will log the error in the log/[your environment].log

with a debug tag alternatively you can also used

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

于 2012-10-28T15:53:26.983 回答