class Serialized
Serialize log messages in a structured format.
Definitions
def initialize(stream, format: Format.default, **options)
Create a new serialized output.
Signature
-
parameter
ioIO The output stream.
-
parameter
formatConsole::Format The format to use for serializing log messages.
-
parameter
optionsHash 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
recordHash 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
subjectString The subject of the log message.
-
parameter
argumentsArray The arguments to log.
-
parameter
severitySymbol The severity of the log message.
-
parameter
optionsHash Additional options.
-
parameter
blockProc 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,
process_id: Process.pid,
fiber_id: Fiber.current.object_id,
}
# For backwards compatibility:
record[:pid] = record[:process_id]
# 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
record[:object_id] = subject.object_id
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