Skip to content

Instantly share code, notes, and snippets.

@DavertMik
Created December 18, 2020 04:28
Show Gist options
  • Save DavertMik/ad2d7246fa95be6100047c6f0a0a5666 to your computer and use it in GitHub Desktop.
Save DavertMik/ad2d7246fa95be6100047c6f0a0a5666 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