Module: BrainzLab::Debug
- Defined in:
- lib/brainzlab/debug.rb
Overview
Debug module for SDK operation logging
Provides pretty-printed debug output for all SDK operations when debug mode is enabled. Includes timing information and request/response details.
Constant Summary collapse
- COLORS =
{ reset: "\e[0m", bold: "\e[1m", dim: "\e[2m", red: "\e[31m", green: "\e[32m", yellow: "\e[33m", blue: "\e[34m", magenta: "\e[35m", cyan: "\e[36m", white: "\e[37m", gray: "\e[90m" }.freeze
- LEVEL_COLORS =
{ debug: :gray, info: :cyan, warn: :yellow, error: :red, fatal: :red }.freeze
- LEVEL_LABELS =
{ debug: 'DEBUG', info: 'INFO', warn: 'WARN', error: 'ERROR', fatal: 'FATAL' }.freeze
Class Method Summary collapse
-
.colorize? ⇒ Boolean
Check if colorized output should be used.
-
.enabled? ⇒ Boolean
Check if debug mode is enabled.
-
.log(message, level: :info, **data) ⇒ void
Log a debug message if debug mode is enabled.
-
.log_operation(service, operation, **data) ⇒ void
Log an SDK operation with timing.
-
.log_request(service, method, path, data: nil) ⇒ void
Log an outgoing request.
-
.log_response(service, status, duration_ms, error: nil) ⇒ void
Log an incoming response.
-
.measure(service, operation) { ... } ⇒ Object
Measure and log execution time of a block.
-
.reset_colorize! ⇒ Object
Reset colorize detection (useful for testing).
Class Method Details
.colorize? ⇒ Boolean
Check if colorized output should be used
159 160 161 162 163 164 |
# File 'lib/brainzlab/debug.rb', line 159 def colorize? return false unless enabled? return @colorize if defined?(@colorize) @colorize = $stdout.tty? end |
.enabled? ⇒ Boolean
Check if debug mode is enabled
152 153 154 |
# File 'lib/brainzlab/debug.rb', line 152 def enabled? BrainzLab.configuration.debug? end |
.log(message, level: :info, **data) ⇒ void
This method returns an undefined value.
Log a debug message if debug mode is enabled
61 62 63 64 65 66 |
# File 'lib/brainzlab/debug.rb', line 61 def log(, level: :info, **data) return unless enabled? output = (, level: level, **data) write_output(output, level: level) end |
.log_operation(service, operation, **data) ⇒ void
This method returns an undefined value.
Log an SDK operation with timing
115 116 117 118 119 120 121 122 123 |
# File 'lib/brainzlab/debug.rb', line 115 def log_operation(service, operation, **data) return unless enabled? data_summary = data.empty? ? '' : " (#{format_data_inline(data)})" = "#{operation}#{data_summary}" output = (:out, service, ) write_output(output, level: :info) end |
.log_request(service, method, path, data: nil) ⇒ void
This method returns an undefined value.
Log an outgoing request
75 76 77 78 79 80 81 82 83 |
# File 'lib/brainzlab/debug.rb', line 75 def log_request(service, method, path, data: nil) return unless enabled? data_summary = summarize_data(data) if data = data_summary ? "#{method} #{path} #{data_summary}" : "#{method} #{path}" output = (:out, service, ) write_output(output, level: :info) end |
.log_response(service, status, duration_ms, error: nil) ⇒ void
This method returns an undefined value.
Log an incoming response
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/brainzlab/debug.rb', line 92 def log_response(service, status, duration_ms, error: nil) return unless enabled? status_text = (status) duration_text = format_duration(duration_ms) = if error "#{status} #{status_text} (#{duration_text}) - #{error}" else "#{status} #{status_text} (#{duration_text})" end level = status >= 400 ? :error : :info output = (:in, service, , level: level) write_output(output, level: level) end |
.measure(service, operation) { ... } ⇒ Object
Measure and log execution time of a block
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/brainzlab/debug.rb', line 131 def measure(service, operation) return yield unless enabled? start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) log_operation(service, operation) result = yield duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(2) log("#{operation} completed", level: :debug, duration_ms: duration_ms, service: service.to_s) result rescue StandardError => e duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(2) log("#{operation} failed: #{e.}", level: :error, duration_ms: duration_ms, service: service.to_s) raise end |
.reset_colorize! ⇒ Object
Reset colorize detection (useful for testing)
167 168 169 |
# File 'lib/brainzlab/debug.rb', line 167 def reset_colorize! remove_instance_variable(:@colorize) if defined?(@colorize) end |