Class: Console::Capture
- Inherits:
-
Object
- Object
- Console::Capture
- Includes:
- Enumerable
- Defined in:
- lib/console/capture.rb
Overview
A buffer which captures all logged messages into a buffer.
Instance Attribute Summary collapse
- #All records captured by this buffer.(recordscapturedbythisbuffer.) ⇒ Object readonly
-
#records ⇒ Object
(also: #buffer, #to_a)
readonly
Returns the value of attribute records.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block) ⇒ Object
Record a log message in the buffer.
-
#clear ⇒ Object
Clear all records from the buffer.
-
#each(&block) ⇒ Object
Iterate over all records in the buffer.
- #empty? ⇒ Boolean
- #first ⇒ Object
- #If true, the buffer will capture verbose messages.=(true, thebufferwillcaptureverbosemessages. = (value)) ⇒ Object
-
#include?(pattern) ⇒ Boolean
Whether the buffer includes any records with the given subject or message pattern.
-
#initialize ⇒ Capture
constructor
Create a new log capture buffer.
- #last ⇒ Object
-
#verbose!(value = true) ⇒ Object
Sets the verbose flag which controls whether verbose messages are captured.
- #verbose? ⇒ Boolean
Constructor Details
#initialize ⇒ Capture
Create a new log capture buffer.
13 14 15 16 |
# File 'lib/console/capture.rb', line 13 def initialize @records = [] @verbose = false end |
Instance Attribute Details
#All records captured by this buffer.(recordscapturedbythisbuffer.) ⇒ Object (readonly)
19 |
# File 'lib/console/capture.rb', line 19 attr :records |
#records ⇒ Object (readonly) Also known as: buffer, to_a
Returns the value of attribute records.
19 20 21 |
# File 'lib/console/capture.rb', line 19 def records @records end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
27 28 29 |
# File 'lib/console/capture.rb', line 27 def verbose @verbose end |
Instance Method Details
#call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block) ⇒ Object
Record a log message in the buffer.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/console/capture.rb', line 87 def call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **, &block) record = { time: ::Time.now.iso8601, severity: severity, **, } 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 |
#clear ⇒ Object
Clear all records from the buffer.
59 60 61 |
# File 'lib/console/capture.rb', line 59 def clear @records.clear end |
#each(&block) ⇒ Object
Iterate over all records in the buffer.
42 43 44 |
# File 'lib/console/capture.rb', line 42 def each(&block) @records.each(&block) end |
#empty? ⇒ Boolean
64 65 66 |
# File 'lib/console/capture.rb', line 64 def empty? @records.empty? end |
#first ⇒ Object
49 50 51 |
# File 'lib/console/capture.rb', line 49 def first @records.first end |
#If true, the buffer will capture verbose messages.=(true, thebufferwillcaptureverbosemessages. = (value)) ⇒ Object
27 |
# File 'lib/console/capture.rb', line 27 attr :verbose |
#include?(pattern) ⇒ Boolean
Whether the buffer includes any records with the given subject or message pattern.
32 33 34 35 36 |
# File 'lib/console/capture.rb', line 32 def include?(pattern) @records.any? do |record| record[:subject].to_s&.match?(pattern) or record[:message].to_s&.match?(pattern) end end |
#last ⇒ Object
54 55 56 |
# File 'lib/console/capture.rb', line 54 def last @records.last end |
#verbose!(value = true) ⇒ Object
Sets the verbose flag which controls whether verbose messages are captured.
69 70 71 |
# File 'lib/console/capture.rb', line 69 def verbose!(value = true) @verbose = value end |
#verbose? ⇒ Boolean
74 75 76 |
# File 'lib/console/capture.rb', line 74 def verbose? @verbose end |