Sha256: a7a14970bd9d9a3bd20e842ecf7ececa8821349540d3ef6c50026ed2ebe09079

Contents?: true

Size: 1.24 KB

Versions: 23

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

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

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

      debug_stream = @options.debug

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

    if !Exception.instance_methods.include?(:full_message)

      def log_exception(ex, level: @options.debug_level, 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, color: color) { message }
      end

    else

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

        log(level: level, color: color) { ex.full_message }
      end

    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
httpx-0.15.1 lib/httpx/loggable.rb
httpx-0.15.0 lib/httpx/loggable.rb
httpx-0.14.5 lib/httpx/loggable.rb
httpx-0.14.4 lib/httpx/loggable.rb
httpx-0.14.3 lib/httpx/loggable.rb
httpx-0.14.2 lib/httpx/loggable.rb
httpx-0.14.1 lib/httpx/loggable.rb
httpx-0.14.0 lib/httpx/loggable.rb
httpx-0.13.2 lib/httpx/loggable.rb
httpx-0.13.1 lib/httpx/loggable.rb
httpx-0.13.0 lib/httpx/loggable.rb
httpx-0.12.0 lib/httpx/loggable.rb
httpx-0.11.3 lib/httpx/loggable.rb
httpx-0.11.2 lib/httpx/loggable.rb
httpx-0.11.1 lib/httpx/loggable.rb
httpx-0.11.0 lib/httpx/loggable.rb
httpx-0.10.2 lib/httpx/loggable.rb
httpx-0.10.1 lib/httpx/loggable.rb
httpx-0.10.0 lib/httpx/loggable.rb
httpx-0.9.0 lib/httpx/loggable.rb