Ruby/Rails debug toolkit
rails_dt gem gives you the DT.p() method to print debug messages.
Usage
In your Gemfile, add:
gem "rails_dt"
#gem "rails_dt", "git: https://github.com/dadooda/rails_dt.git" # Edge version.Now, in your code, do something like:
DT.p "checkpoint 1"
DT.p "user", userDebug messages are printed to:
-
Rails.loggerin Rails mode (auto-detected); -
STDERRin non-Rails mode; -
log/dt.logiflog/exists in project root and is writable.
This is often handy:
$ tail -f log/dt.log
The ideas behind it
- Debug message printer must not require initialization.
- Debug message printer must be nothing else, but a debug message printer.
- Debug message printer must be invoked the same way regardless of place of invocation.
- Debug message printer calls must be clearly visible in code.
- Debug message printer must print its location in code so you can easily remove the call once debugging is over.
A few out-of-the-box implementations review
Let me check a few popular out-of-the box implementation used by many of developers against "the ideas" items listed above.
Rails.logger:
- Fail. It only works in Rails. Rails requires initialization.
- (!) Fail. Logger is a production facility.
- So-so. It's not possible to use Rails logger to debug parts of Rails itself.
- (!) Fail. Debugging logger calls look the same as production logger calls.
- Fail. Location in code is not printed.
Kernel::p:
- OK.
- OK.
- OK.
- So-so.
pcalls hide well among lines of meaningful code and it isn't always easy to spot them. - Fail. Location in code is not printed.
Cheers!
Feedback of any kind is greatly appreciated.
— Alex Fortuna, © 2010-2017