Created
October 15, 2018 19:58
-
-
Save eiwi1101/482e276f13c31c2b9a3725c5796304d5 to your computer and use it in GitHub Desktop.
Log validation errors and changes for all ActiveRecord updates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationRecord << ActiveRecord::Base | |
#... | |
after_validation :log_validation_errors | |
private | |
def log_validation_errors | |
Rails.logger.tagged self.class.name do | |
if self.errors.empty? | |
Rails.logger.debug "Record is valid: #{self.inspect}" | |
Rails.logger.debug '- Changes:' | |
self.changes.each { |k,c| Rails.logger.debug " * #{k}: #{c.collect(&:inspect).join(' => ')}" } | |
else | |
Rails.logger.warn "Record is invalid: #{self.inspect}" | |
Rails.logger.warn '- Failed validation:' | |
self.errors.full_messages.each { |m| Rails.logger.info " * #{m}" } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment