Skip to content

Instantly share code, notes, and snippets.

@phil-monroe
Last active September 28, 2020 19:06
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 phil-monroe/237328ddf2fb4e509dd3ef9e5fca33b7 to your computer and use it in GitHub Desktop.
Save phil-monroe/237328ddf2fb4e509dd3ef9e5fca33b7 to your computer and use it in GitHub Desktop.
Debug Logging for Rails Autoloading
# Include this in the config/application.rb of your rails application
require 'colorize'
ActiveSupport::Dependencies.singleton_class.prepend(Module.new do
def load_missing_constant(*args)
arg_str = args.map(&:to_s).select { |arg| arg != 'Object' }.join('::').green
start = Time.now
result = nil
mv_with_indent do |indent|
indent_space = ' ' * indent * 4
puts "AUTOLOADING START: #{indent_space} +-> #{arg_str}"
begin
result = super
puts "AUTOLOADING DONE : #{indent_space} +-> #{arg_str} -- #{mv_duration_string(start)}"
rescue StandardError => e
puts "AUTOLOADING FAIL : #{indent_space} +-> #{arg_str} #{e} -- #{mv_duration_string(start)}"
raise
end
end
result
end
def mv_with_indent
Thread.current[:_auto_reload_indent] ||= 0
Thread.current[:_auto_reload_indent] += 1
yield Thread.current[:_auto_reload_indent]
ensure
Thread.current[:_auto_reload_indent] -= 1
end
def mv_duration_string(start, finish: Time.now)
duration = (finish - start) * 1000.0
duration_str = "#{duration.round(2)}ms"
if duration > 10
duration_str.red
elsif duration > 1
duration_str.yellow
else
duration_str
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment