Class: HTTP::Features::Logging::BodyLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/http/features/logging.rb

Overview

Stream wrapper that logs each chunk as it flows through readpartial

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, logger, formatter: nil) ⇒ BodyLogger

Create a new BodyLogger wrapping a stream

Examples:

BodyLogger.new(stream, logger)

Parameters:

  • stream (#readpartial)

    the stream to wrap

  • logger (#debug)

    the logger instance

  • formatter (#call, nil) (defaults to: nil)

    optional formatter for each chunk



208
209
210
211
212
213
# File 'lib/http/features/logging.rb', line 208

def initialize(stream, logger, formatter: nil)
  @stream = stream
  @connection = stream.respond_to?(:connection) ? stream.connection : stream
  @logger = logger
  @formatter = formatter
end

Instance Attribute Details

#connectionHTTP::Connection (readonly)

The underlying connection

Examples:

body_logger.connection

Returns:



196
197
198
# File 'lib/http/features/logging.rb', line 196

def connection
  @connection
end

Instance Method Details

#readpartialString

Read a chunk from the underlying stream and log it

Examples:

body_logger.readpartial # => "chunk"

Returns:

  • (String)

    the chunk read from the stream

Raises:

  • (EOFError)

    when no more data left



223
224
225
226
227
# File 'lib/http/features/logging.rb', line 223

def readpartial(*)
  chunk = @stream.readpartial(*)
  @logger.debug { @formatter ? @formatter.call(chunk) : chunk } # steep:ignore
  chunk
end