DebugTracer
A Ruby gem for debug logging that outputs variable values with execution context to assist AI-powered bug detection.
Features
- Automatic Context Capture: Automatically captures execution context using caller_locations
- Flexible Configuration: Customize output destination and display options via YAML configuration
-
Simple API: Output variable values instantly with
dt(binding, :var) - Thread-Safe: Safe operation in multi-threaded environments
Installation
Add to your Gemfile:
gem 'debug-tracer'Then execute:
$ bundle installOr install directly:
$ gem install debug-tracerRails Setup
For Rails applications, run the install generator:
$ bundle exec rails generate debug_tracer:installThis will create:
-
config/initializers/debug_tracer.rb- Initializer with default configuration -
config/debug-tracer.yml- YAML configuration file
Basic Usage
require 'debug-tracer'
class Order
include DebugTracer
def apply_coupon(price, coupon)
dt(binding, :price, :coupon)
# Output:
# /path/to/order.rb:7 in `apply_coupon`
# => price = 1000
# => coupon = #<Coupon id: 1, discount_rate: 20>
discount = price * (coupon.discount_rate / 100.0)
dt(binding, :discount)
# => discount = 200.0
final_price = price - discount
dt(binding, :final_price)
# => final_price = 800.0
final_price
end
endConfiguration
Programmatic Configuration
DebugTracer.configure do |config|
config.enabled = true
config.output_target = :stdout # or :file
config.output_file_path = 'log/debug_trace.log'
config.display_file_path = true
config.display_line_number = true
config.display_method_name = true
endYAML Configuration File
config/debug-tracer.yml:
enabled: true
output:
target: stdout # stdout or file
file_path: log/debug_trace.log
display:
file_path: true
line_number: true
method_name: trueLoad configuration file:
DebugTracer.load_config_file('config/debug-tracer.yml')Rails Usage
After running rails generate debug_tracer:install, the initializer is automatically configured:
config/initializers/debug_tracer.rb:
DebugTracer.configure do |config|
config.enabled = Rails.env.development?
config.output_target = :file
config.output_file_path = Rails.root.join('log', 'debug_trace.log')
config.display_file_path = true
config.display_line_number = true
config.display_method_name = true
endConfiguration Options
| Option | Default | Description |
|---|---|---|
enabled |
true |
Enable/disable debug tracing |
output_target |
:stdout |
Output destination (:stdout or :file) |
output_file_path |
'log/debug_trace.log' |
File path for file output |
display_file_path |
true |
Display file path |
display_line_number |
true |
Display line number |
display_method_name |
true |
Display method name |
AI-Powered Bug Detection
DebugTracer's output is optimized for AI-powered bug analysis:
- Execution Context: Identify problem locations with file path, line number, and method name
- Variable State: Track variable values at each point in time
- Structured Output: Consistent format that's easy for AI to parse
Troubleshooting
Variables showing as (undefined)
- Check the spelling of variable names
- Ensure dt is called within the scope of local variables
File output not working
- Check write permissions for the output directory
- Verify the file path is correctly configured
Development
Bug reports and pull requests are welcome on GitHub.
License
This gem is available as open source under the terms of the MIT License.