Module: HTTPX::Loggable

Included in:
Connection, Connection::HTTP1, Connection::HTTP2, ErrorResponse, Resolver::ResolverMixin, Session, TCP, UDP
Defined in:
lib/httpx/loggable.rb

Constant Summary collapse

COLORS =
{
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
}.freeze

Instance Method Summary collapse

Instance Method Details

#log(level: @options.debug_level, label: "", color: nil, &msg) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/httpx/loggable.rb', line 16

def log(level: @options.debug_level, label: "", color: nil, &msg)
  return unless @options.debug
  return unless @options.debug_level >= level

  debug_stream = @options.debug

  message = (+label << msg.call << "\n")
  message = "\e[#{COLORS[color]}m#{message}\e[0m" if debug_stream.respond_to?(:isatty) && debug_stream.isatty
  debug_stream << message
end

#log_exception(ex, level: @options.debug_level, label: "", color: nil) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/httpx/loggable.rb', line 29

def log_exception(ex, level: @options.debug_level, label: "", color: nil)
  return unless @options.debug
  return unless @options.debug_level >= level

  message = +"#{ex.message} (#{ex.class})"
  message << "\n" << ex.backtrace.join("\n") unless ex.backtrace.nil?
  log(level: level, label: label, color: color) { message }
end