Sha256: 2e4c1c1db3008614a89459d2a5b5882f58a0c22326a04f4ce0abc6606dc34dff

Contents?: true

Size: 1.25 KB

Versions: 44

Compression:

Stored size: 1.25 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 color && 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

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

    else

      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

    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
httpx-0.24.7 lib/httpx/loggable.rb
httpx-0.24.6 lib/httpx/loggable.rb
httpx-0.24.5 lib/httpx/loggable.rb
httpx-0.24.4 lib/httpx/loggable.rb
httpx-0.24.3 lib/httpx/loggable.rb
httpx-0.24.2 lib/httpx/loggable.rb
httpx-0.24.1 lib/httpx/loggable.rb
httpx-0.24.0 lib/httpx/loggable.rb
httpx-0.23.4 lib/httpx/loggable.rb
httpx-0.23.3 lib/httpx/loggable.rb
httpx-0.23.2 lib/httpx/loggable.rb
httpx-0.23.1 lib/httpx/loggable.rb
httpx-0.23.0 lib/httpx/loggable.rb
httpx-0.22.5 lib/httpx/loggable.rb
httpx-0.22.4 lib/httpx/loggable.rb
httpx-0.22.3 lib/httpx/loggable.rb
httpx-0.22.2 lib/httpx/loggable.rb
httpx-0.22.1 lib/httpx/loggable.rb
httpx-0.22.0 lib/httpx/loggable.rb
httpx-0.21.1 lib/httpx/loggable.rb