Class: Lumberjack::CaptureDevice
- Inherits:
-
Device
- Object
- Device
- Lumberjack::CaptureDevice
- Defined in:
- lib/lumberjack_capture_device.rb
Overview
Lumberjack device for capturing log entries into memory to allow them to be inspected for testing purposes.
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Class Method Summary collapse
-
.capture(logger) ⇒ Object
Capture the entries written by the logger within a block.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all entries that have been written to the buffer.
-
#extract(message: nil, level: nil, tags: nil, limit: nil) ⇒ Object
Return all the captured entries that match the specified filters.
-
#include?(message: nil, level: nil, tags: nil) ⇒ Boolean
Return true if the captured log entries match the specified level, message, and tags.
-
#initialize ⇒ CaptureDevice
constructor
A new instance of CaptureDevice.
- #write(entry) ⇒ Object
Constructor Details
#initialize ⇒ CaptureDevice
Returns a new instance of CaptureDevice.
36 37 38 |
# File 'lib/lumberjack_capture_device.rb', line 36 def initialize @buffer = [] end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
9 10 11 |
# File 'lib/lumberjack_capture_device.rb', line 9 def buffer @buffer end |
Class Method Details
.capture(logger) ⇒ Object
Capture the entries written by the logger within a block. Within the block all log entries will be written to a CaptureDevice rather than to the normal output for the logger. In addition, all formatters will be removed and the log level will be set to debug. The device being written to be both yielded to the block as well as returned by the method call.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lumberjack_capture_device.rb', line 17 def capture(logger) device = new save_device = logger.device save_level = logger.level save_formatter = logger.formatter begin logger.device = device logger.level = :debug logger.formatter = Lumberjack::Formatter.empty yield device ensure logger.device = save_device logger.level = save_level logger.formatter = save_formatter end device end |
Instance Method Details
#clear ⇒ Object
Clear all entries that have been written to the buffer.
45 46 47 |
# File 'lib/lumberjack_capture_device.rb', line 45 def clear @buffer.clear end |
#extract(message: nil, level: nil, tags: nil, limit: nil) ⇒ Object
Return all the captured entries that match the specified filters. These filters are the same as described in the include? method.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lumberjack_capture_device.rb', line 73 def extract(message: nil, level: nil, tags: nil, limit: nil) matches = [] if level # Normalize the level filter to numeric values. level = (level.is_a?(Integer) ? level : Lumberjack::Severity.label_to_level(level)) end @buffer.each do |entry| if matched?(entry, , level, ) matches << entry break if limit && matches.size >= limit end end matches end |
#include?(message: nil, level: nil, tags: nil) ⇒ Boolean
Return true if the captured log entries match the specified level, message, and tags.
For level, you can specified either a numeric constant (i.e. Logger::WARN) or a symbol (i.e. :warn).
For message you can specify a string to perform an exact match or a regular expression to perform a partial or pattern match. You can also supply any matcher value available in your test library (i.e. in rspec you could use anything or ‘instance_of(Error)`, etc.).
For tags, you can specify a hash of tag names to values to match. You can use regular expression or matchers as the values here as well. Tags can also be nested to match nested tags.
Example:
“‘ logs.include(level: :warn, message: /something happened/, tags: instance_of(Float)) “`
67 68 69 |
# File 'lib/lumberjack_capture_device.rb', line 67 def include?(message: nil, level: nil, tags: nil) !extract(message: , level: level, tags: , limit: 1).empty? end |
#write(entry) ⇒ Object
40 41 42 |
# File 'lib/lumberjack_capture_device.rb', line 40 def write(entry) @buffer << entry end |