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

1 Answer 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} "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.