Class: LoggerForTest

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/logger_for_test.rb

Overview

This is the implementation of Logger that stores an in memory representation of your log lines. It uses the delegator pattern to ensure that things continue to work suggested usages: LoggerForTest.new(Logger.new(STDOUT)) LoggerForTest.new(Rails.logger))

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ LoggerForTest

Returns a new instance of LoggerForTest.



12
13
14
15
# File 'lib/logger_for_test.rb', line 12

def initialize(*args)
  super
  @log_lines = []
end

Instance Attribute Details

#log_linesObject

Returns the value of attribute log_lines.



16
17
18
# File 'lib/logger_for_test.rb', line 16

def log_lines
  @log_lines
end

Instance Method Details

#add(sev, message, progname = nil) ⇒ Object



18
19
20
21
# File 'lib/logger_for_test.rb', line 18

def add(sev, message, progname = nil)
  log_lines << format(message, sev, progname)
  super(sev, message, progname)
end

#contains_log?(message, sev = nil, progname = nil) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/logger_for_test.rb', line 23

def contains_log?(message, sev = nil, progname = nil)
  filtered_by(sev, progname).any? do |line|
    line.fetch(:message) =~ Regexp.new(message)
  end
end