Configuration¶
DebugMe provides extensive configuration options through the DebugMeDefaultOptions hash and per-call options. You can configure defaults once for your entire application, eliminating the need to pass options on every call.
Including the Module¶
The debug_me method is an instance method of the DebugMe module, available inside classes that include the module (or objects that extend it). It can also be called without any include as DebugMe.debug_me; the block is still evaluated in the caller's context, so local, instance, and class variables and constants come from where the call is made.
Include at the Top Level (Object)¶
Include at the top level to add DebugMe to Object, making debug_me available throughout your application:
require 'debug_me'
include DebugMe # At top level, this includes DebugMe into Object
# Now available globally
debug_me { :anywhere }
Include at Application Level¶
In a Rails application, include in ApplicationController or ApplicationRecord:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include DebugMe
end
# Now available in all controllers
class UsersController < ApplicationController
def show
debug_me { :params }
end
end
Include in Specific Classes¶
For more targeted usage, include only where needed:
class PaymentProcessor
include DebugMe
def process(amount)
debug_me('PAYMENT') { :amount }
end
end
class OtherClass
# debug_me is not available here
end
Application-Wide Configuration¶
Configure defaults once so every debug_me call uses your preferred settings.
Rails Initializer¶
Create config/initializers/debug_me.rb:
require 'debug_me'
# Include globally (optional - see "Including the Module" above)
include DebugMe
# Disable in production
$DEBUG_ME = !Rails.env.production?
# Configure defaults for all debug_me calls
DebugMeDefaultOptions[:tag] = 'MYAPP'
DebugMeDefaultOptions[:logger] = Rails.logger
DebugMeDefaultOptions[:time] = true
DebugMeDefaultOptions[:levels] = 0
# Or merge multiple options at once
DebugMeDefaultOptions.merge!(
tag: 'MYAPP',
logger: Rails.logger,
header: true,
time: true
)
Now every debug_me call uses these defaults:
# Uses tag 'MYAPP', logs to Rails.logger, etc.
debug_me { :user }
# Override specific options when needed
debug_me('PAYMENT', levels: 3) { :amount }
Plain Ruby Application¶
Create a configuration file that's required early:
# lib/debug_config.rb
require 'debug_me'
include DebugMe
$DEBUG_ME = ENV['DEBUG'] == 'true'
DebugMeDefaultOptions[:tag] = 'APP'
DebugMeDefaultOptions[:time] = true
DebugMeDefaultOptions[:file] = $stderr
# main.rb
require_relative 'lib/debug_config'
# debug_me is now configured and available
debug_me { :starting_up }
Default Options¶
The gem ships with these default settings:
DebugMeDefaultOptions = {
tag: 'DEBUG', # Label prepended to output
time: true, # Include timestamp
strftime: '%Y-%m-%d %H:%M:%S.%6N', # Timestamp format
header: true, # Show source file and line
levels: 0, # Backtrace levels to display
skip1: false, # Skip 1 blank line before output
skip2: false, # Skip 2 blank lines before output
lvar: true, # Include local variables
ivar: true, # Include instance variables
cvar: true, # Include class variables
cconst: true, # Include class constants
logger: nil, # Logger instance
file: $stdout # Output destination
}
Option Reference¶
Output Control¶
tag (String)¶
The label prepended to each debug output line.
debug_me(tag: 'INFO') { :status }
# 2026-11-09 12:30:45.123456 INFO Source: example.rb:1:in '<main>'
debug_me(tag: '>>> CHECK') { :value }
# 2026-11-09 12:30:45.123456 >>> CHECK Source: example.rb:1:in '<main>'
Shorthand Syntax
You can pass a string as the first argument instead of using tag::
time (Boolean)¶
Whether to include a timestamp in the output.
strftime (String)¶
Format string for timestamps (uses Ruby's strftime).
debug_me(strftime: '%H:%M:%S') { :now }
# 12:30:45 DEBUG Source: example.rb:1:in '<main>'
debug_me(strftime: '%Y-%m-%d') { :today }
# 2026-11-09 DEBUG Source: example.rb:1:in '<main>'
header (Boolean)¶
Whether to print the header line with source location.
levels (Integer)¶
Number of backtrace levels to include in output.
debug_me(levels: 3) { :data }
# 2026-11-09 12:30:45.123456 DEBUG Source: example.rb:5:in 'Object#method_a'
# 2026-11-09 12:30:45.123456 DEBUG backtrace -=> [[0, "example.rb:7:in 'Object#method_b'"],
# [1, "example.rb:8:in 'Object#method_c'"],
# [2, "example.rb:9:in '<main>'"]]
# 2026-11-09 12:30:45.123456 DEBUG data -=> "value"
skip1 / skip2 (Boolean)¶
Add blank lines before output for visual separation.
debug_me(skip1: true) { :step1 }
# (one blank line)
# 2026-11-09 12:30:45.123456 DEBUG Source: example.rb:1:in '<main>'
debug_me(skip2: true) { :step2 }
# (two blank lines)
# 2026-11-09 12:30:45.123456 DEBUG Source: example.rb:1:in '<main>'
Variable Selection¶
These options control which variables are shown when using an empty block (debug_me {}):
lvar (Boolean)¶
Include local variables. Default: true
ivar (Boolean)¶
Include instance variables. Default: true
cvar (Boolean)¶
Include class variables. Default: true
cconst (Boolean)¶
Include class constants. Default: true
Output Destination¶
file (IO)¶
The output destination. Defaults to $stdout.
# Output to STDERR
debug_me(file: $stderr) { :error }
# Output to a file
log_file = File.open('debug.log', 'a')
debug_me(file: log_file) { :data }
log_file.close
logger (Logger)¶
A Ruby Logger instance for integration with logging systems.
For Rails applications:
Global Configuration¶
Modifying Defaults¶
You can modify the default options globally:
# Change default tag
DebugMeDefaultOptions[:tag] = 'TRACE'
# Disable timestamps by default
DebugMeDefaultOptions[:time] = false
# Always show 2 backtrace levels
DebugMeDefaultOptions[:levels] = 2
The $DEBUG_ME Flag¶
Control all debug output globally:
When $DEBUG_ME is false:
- No output is produced
- The debug block is not evaluated
- Zero performance overhead
Environment Variable¶
Set DEBUG_ME in your environment:
Supported values:
| Enable | Disable |
|---|---|
true |
false |
yes |
no |
on |
off |
1 |
0 |
Configuration Patterns¶
Development vs Production¶
# config/initializers/debug_me.rb
if defined?(DebugMe)
include DebugMe
$DEBUG_ME = Rails.env.development? || Rails.env.test?
DebugMeDefaultOptions[:logger] = Rails.logger
DebugMeDefaultOptions[:tag] = Rails.env.upcase
end