If you want a piece of code to print messages to the console only in a debug phase, is there a better alternative to;

  • Defining a global const DEBUG in its own file.
DEBUG = true
  • importing (requiring) it in all concerned code files.
  • printing conditionally:
if DEBUG
    puts "Oops!"
end

Use log module.

require "log"

Log.setup(:debug)

Log.debug {"Oops!"}
2 Likes

Perfect. Thank you!

Also, you can try GitHub - Sija/debug.cr: Debug macro for Crystal

7 Likes

That’s beautiful!!