ConsoleSourceConsoleOutputSerialized

class Serialized

Serialize log messages in a structured format.

Definitions

def initialize(stream, format: Format.default, **options)

Create a new serialized output.

Signature

parameter io IO

The output stream.

parameter format Console::Format

The format to use for serializing log messages.

parameter options Hash

Additional options to customize the output.

Implementation

def initialize(stream, format: Format.default, **options)
	@stream = stream
	@format = format
end

def last_output

This a final output that then writes to an IO object.

Implementation

def last_output
	self
end

attr :stream

Signature

attribute IO

The output stream.

attr :format

Signature

attribute Console::Format

The format to use for serializing log messages.

def dump(record)

Serialize the given record.

Signature

parameter record Hash

The record to serialize.

returns String

The serialized record.

Implementation

def dump(record)
	@format.dump(record)
end

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

Output the given log message.

Signature

parameter subject String

The subject of the log message.

parameter arguments Array

The arguments to log.

parameter severity Symbol

The severity of the log message.

parameter options Hash

Additional options.

parameter block Proc

An optional block used to generate the log message.

Implementation

def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
	record = {
		time: Time.now.iso8601,
		severity: severity,
		oid: subject.object_id,
		pid: Process.pid,
	}
	
	# We want to log just a brief subject:
	if subject.is_a?(String)
		record[:subject] = subject
	elsif subject.is_a?(Module)
		record[:subject] = subject.name
	else
		record[:subject] = subject.class.name
	end
	
	if annotation = Fiber.current.annotation
		record[:annotation] = annotation
	end
	
	message = arguments
	
	if block_given?
		if block.arity.zero?
			message << yield
		else
			buffer = StringIO.new
			yield buffer
			message << buffer.string
		end
	end
	
	if message.size == 1
		record[:message] = message.first
	elsif message.any?
		record[:message] = message
	end
	
	record.update(options)
	
	@stream.write(self.dump(record) << "\n")
end