Skip to content

Instantly share code, notes, and snippets.

@ajvargo
Created November 7, 2018 19:51
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 ajvargo/7b51b831a83a35420afd3d834e135f3c to your computer and use it in GitHub Desktop.
Save ajvargo/7b51b831a83a35420afd3d834e135f3c to your computer and use it in GitHub Desktop.
refactoring utils
# config/initializers/ad_refactoring_utils.rb
module Kernel
def print_error_for_debug(e)
puts "E" * 72
puts "-->#{caller.first}"
p e
e.backtrace.delete_if do |l|
l.match('gems').present? ||
l.match('ruby').present?
end.first(3).each{|b| puts b}
puts "E" * 72
end
end
class MethodTraceLogger
def self.logger
@logger ||= init_logger
end
def self.log
return unless ["test", "development"].include? Rails.env
locations = caller_locations.select do |c|
c.path.match(/lello/) &&
!c.path.match('spec_helper') &&
!c.path.match('vendor/bundle') &&
!c.path.match('bad_uri_handler') &&
!c.path.match('multipart_sanitizer_middleware') &&
!c.path.match('bad_json_handler')
end
invoker = locations.first.label
_, first, *rest = locations
return unless first
logger.tagged(invoker){logger.info "---#{first.path}:#{first.lineno}" }
rest.each do |loc|
logger.tagged(invoker){ logger.info "#{loc.path}:#{loc.lineno}" }
end
end
def self.init_logger
lgr = if ENV['CIRCLE_ARTIFACTS'].present?
Logger.new(File.join(ENV['CIRCLE_ARTIFACTS'], "method_trace.log"))
elsif ["test", "development"].include? Rails.env
Logger.new(File.join(Rails.root, "log", "method_trace.log"))
else
Logger.new('/dev/null')
end
ActiveSupport::TaggedLogging.new lgr
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment