Skip to content

Instantly share code, notes, and snippets.

@eiwi1101
Created October 15, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiwi1101/482e276f13c31c2b9a3725c5796304d5 to your computer and use it in GitHub Desktop.
Save eiwi1101/482e276f13c31c2b9a3725c5796304d5 to your computer and use it in GitHub Desktop.
Log validation errors and changes for all ActiveRecord updates.
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