Module: RSpec::LoggingHelper

Included in:
Core::Configuration
Defined in:
lib/rspec/logging_helper.rb

Instance Method Summary collapse

Instance Method Details

#capture_log_messages(opts = {}) ⇒ Object

Capture log messages from the Logging framework and make them available via a @log_output instance variable. The @log_output supports a readline method to access the log messages.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rspec/logging_helper.rb', line 9

def capture_log_messages( opts = {} )
  from = opts.fetch(:from, 'root')
  to = opts.fetch(:to, '__rspec__')
  exclusive = opts.fetch(:exclusive, true)

  appender = Logging::Appenders[to] || Logging::Appenders::StringIo.new(to)
  logger = Logging::Logger[from]
  if exclusive
    logger.appenders = appender
  else
    logger.add_appenders(appender)
  end

  before(:all) do
    @log_output = Logging::Appenders[to]
  end

  before(:each) do
    @log_output.reset
  end
end