Class: Console::Output::Serialized
- Inherits:
-
Object
- Object
- Console::Output::Serialized
- Defined in:
- lib/console/output/serialized.rb
Overview
Serialize log messages in a structured format.
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
- #The format to use for serializing log messages.(formattouse) ⇒ Object readonly
Instance Method Summary collapse
-
#call(subject = nil, *arguments, severity: UNKNOWN, **options, &block) ⇒ Object
Output the given log message.
-
#dump(record) ⇒ Object
Serialize the given record.
-
#initialize(stream, format: Format.default, **options) ⇒ Serialized
constructor
Create a new serialized output.
-
#last_output ⇒ Object
This a final output that then writes to an IO object.
- #The output stream.=(outputstream. = (value)) ⇒ Object
Constructor Details
#initialize(stream, format: Format.default, **options) ⇒ Serialized
Create a new serialized output.
19 20 21 22 |
# File 'lib/console/output/serialized.rb', line 19 def initialize(stream, format: Format.default, **) @stream = stream @format = format end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
33 34 35 |
# File 'lib/console/output/serialized.rb', line 33 def format @format end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
30 31 32 |
# File 'lib/console/output/serialized.rb', line 30 def stream @stream end |
#The format to use for serializing log messages.(formattouse) ⇒ Object (readonly)
33 |
# File 'lib/console/output/serialized.rb', line 33 attr :format |
Instance Method Details
#call(subject = nil, *arguments, severity: UNKNOWN, **options, &block) ⇒ Object
Output the given log message.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/console/output/serialized.rb', line 50 def call(subject = nil, *arguments, severity: UNKNOWN, **, &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 = arguments if block_given? if block.arity.zero? << yield else buffer = StringIO.new yield buffer << buffer.string end end if .size == 1 record[:message] = .first elsif .any? record[:message] = end record.update() @stream.write(self.dump(record) << "\n") end |
#dump(record) ⇒ Object
Serialize the given record.
39 40 41 |
# File 'lib/console/output/serialized.rb', line 39 def dump(record) @format.dump(record) end |
#last_output ⇒ Object
This a final output that then writes to an IO object.
25 26 27 |
# File 'lib/console/output/serialized.rb', line 25 def last_output self end |
#The output stream.=(outputstream. = (value)) ⇒ Object
30 |
# File 'lib/console/output/serialized.rb', line 30 attr :stream |