ConsoleSourceConsoleCapture

class Capture

A buffer which captures all logged messages into a buffer.

Definitions

def initialize

Create a new log capture buffer.

Implementation

def initialize
	@records = []
	@verbose = false
end

attr :records

Signature

attribute Array(Hash)

All records captured by this buffer.

attr :verbose

Signature

attribute Boolean

If true, the buffer will capture verbose messages.

def include?(pattern)

Whether the buffer includes any records with the given subject or message pattern.

Signature

returns Boolean

True if the buffer includes any records with the given pattern.

Implementation

def include?(pattern)
	@records.any? do |record|
		record[:subject].to_s&.match?(pattern) or record[:message].to_s&.match?(pattern)
	end
end

def each(&block)

Iterate over all records in the buffer.

Signature

yields {|record| ...}

each record in the buffer.

parameter record Hash

The record itself.

Implementation

def each(&block)
	@records.each(&block)
end

def first

Signature

returns Hash

The first record in the buffer.

Implementation

def first
	@records.first
end

def last

Signature

returns Hash

The last record in the buffer.

Implementation

def last
	@records.last
end

def clear

Clear all records from the buffer.

Implementation

def clear
	@records.clear
end

def empty?

Signature

returns Boolean

True if the buffer is empty.

Implementation

def empty?
	@records.empty?
end

def verbose!(value = true)

Sets the verbose flag which controls whether verbose messages are captured.

Implementation

def verbose!(value = true)
	@verbose = value
end

def verbose?

Signature

returns Boolean

True if the buffer is capturing verbose messages.

Implementation

def verbose?
	@verbose
end

def call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block)

Record a log message in the buffer.

Signature

parameter subject Object

The subject of the log message.

parameter arguments Array

The arguments to the log message.

parameter severity Symbol

The severity of the log message.

parameter event Event

The event associated with the log message.

parameter options Hash

Additional options to pass to the log message.

yields {|buffer| ...}

A block which can be used to write additional information to the log message.

parameter buffer IO

The (optional) buffer to write to.

Implementation

def call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block)
	record = {
		time: ::Time.now.iso8601,
		severity: severity,
		**options,
	}
	
	if subject
		record[:subject] = subject
	end
	
	if event
		record[:event] = event.to_hash
	end
	
	if arguments.any?
		record[:arguments] = arguments
	end
	
	if annotation = Fiber.current.annotation
		record[:annotation] = annotation
	end
	
	if block_given?
		if block.arity.zero?
			record[:message] = yield
		else
			buffer = StringIO.new
			yield buffer
			record[:message] = buffer.string
		end
	else
		record[:message] = arguments.join(" ")
	end
	
	@records << record
end