Skip to content

Instantly share code, notes, and snippets.

@Mayurifag
Forked from DavertMik/test_helper.rb
Created January 26, 2022 14:10
Show Gist options
  • Save Mayurifag/ff7936bec89e2e10bd94dbdb6ff62f19 to your computer and use it in GitHub Desktop.
Save Mayurifag/ff7936bec89e2e10bd94dbdb6ff62f19 to your computer and use it in GitHub Desktop.
Better debugging for rails tests: logs and custom output to stdout
# run it as DEBUG=true rails test
if ENV['DEBUG']
Rails.logger = Logger.new(STDOUT)
Rails.logger.level = Logger::INFO
Rails.logger.datetime_format = ""
Rails.logger.formatter = proc do |severity, _time, _progname, msg|
"#{severity.green}: #{msg}\n"
end
# to see database queries
# run it as DEBUG=db rails test
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT) if ENV['DEBUG'] == 'db'
end
# special output which works in debug
def echo(msg)
return unless ENV['DEBUG']
puts msg.blue
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment