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.
28 29 30 |
# File 'lib/console/capture.rb', line 28 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.
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 124 |
# File 'lib/console/capture.rb', line 88 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.
60 61 62 |
# File 'lib/console/capture.rb', line 60 def clear @records.clear end |
#each(&block) ⇒ Object
Iterate over all records in the buffer.
43 44 45 |
# File 'lib/console/capture.rb', line 43 def each(&block) @records.each(&block) end |
#empty? ⇒ Boolean
65 66 67 |
# File 'lib/console/capture.rb', line 65 def empty? @records.empty? end |
#first ⇒ Object
50 51 52 |
# File 'lib/console/capture.rb', line 50 def first @records.first end |
#If true, the buffer will capture verbose messages.=(true, thebufferwillcaptureverbosemessages. = (value)) ⇒ Object
28 |
# File 'lib/console/capture.rb', line 28 attr :verbose |
#include?(pattern) ⇒ Boolean
Whether the buffer includes any records with the given subject or message pattern.
33 34 35 36 37 |
# File 'lib/console/capture.rb', line 33 def include?(pattern) @records.any? do |record| record[:subject].to_s&.match?(pattern) or record[:message].to_s&.match?(pattern) end end |
#last ⇒ Object
55 56 57 |
# File 'lib/console/capture.rb', line 55 def last @records.last end |
#verbose!(value = true) ⇒ Object
Sets the verbose flag which controls whether verbose messages are captured.
70 71 72 |
# File 'lib/console/capture.rb', line 70 def verbose!(value = true) @verbose = value end |
#verbose? ⇒ Boolean
75 76 77 |
# File 'lib/console/capture.rb', line 75 def verbose? @verbose end |