Created
June 9, 2016 12:22
-
-
Save omnikron/740fc9a85a3d87da518158b8a40080e6 to your computer and use it in GitHub Desktop.
Elegantly logs rails params in aligned `key: value` form
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
module ElegantLog | |
extend ActiveSupport::Concern | |
def elegant_log(params, display_name = nil) | |
return if params.blank? | |
spacer | |
Rails.logger.info display_name || params.delete(:controller).titleize | |
longest_length = params.keys.sort {|k1, k2| k2.length <=> k1.length}.first.length | |
params.each do |key, value| | |
display_aligned(key, value, longest_length) | |
end | |
spacer | |
end | |
private | |
def display_aligned(key, value, longest_length) | |
Rails.logger.info "#{key}:#{' ' * (longest_length - key.length)} #{value.inspect}" | |
end | |
def spacer | |
Rails.logger.info star_line << star_line << "\n" | |
end | |
def star_line | |
"\n" << "*" * 50 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stick it in
app/controllers/concerns
and include in individual controllers (or your application controller) likethen log params like magic with
elegant_log(params, 'optional display title')
(default display title is the controller name).Sample output: