Module: Lumberjack::CaptureDevice::RSpec

Defined in:
lib/lumberjack/capture_device/rspec.rb

Overview

RSpec helper methods for working with CaptureDevice.

Instance Method Summary collapse

Instance Method Details

#capture_logger(logger, write_to_original: true) {|device| ... } ⇒ Lumberjack::CaptureDevice

Capture log entries from a logger within a block. This method temporarily replaces the logger’s device with a CaptureDevice, sets the log level to debug, and removes formatters to capture raw log entries for testing.

Examples:

logs = capture_logger(Rails.logger) do
  Rails.logger.info("Test message")
end
expect(logs).to include_log_entry(level: :info, message: "Test message")

Parameters:

  • logger (Lumberjack::Logger)

    The logger to capture entries from.

Yields:

  • (device)

    The block to execute while capturing log entries.

Yield Parameters:

Returns:



40
41
42
# File 'lib/lumberjack/capture_device/rspec.rb', line 40

def capture_logger(logger, write_to_original: true, &block)
  Lumberjack::CaptureDevice.capture(logger, write_to_original: write_to_original, &block)
end

#capture_logger_around_example(logger, example) ⇒ Object

RSpec around hook to automatically capture logs for each example. The captured logs are only written to the original logger if the example fails. This helps keep the logs more usable for debugging test failures since it removes all the noise from passing tests.

This is designed for CI environments where you can save the logs as artifacts of the test run.

Examples:

Capture logs for a Rails application

# In your spec_helper.rb or rails_helper.rb
RSpec.configure do |config|
  config.around do |example|
    capture_logger_around_example(Rails.logger, example)
  end
end

Parameters:

  • logger (Lumberjack::Logger)

    The logger to capture entries for.

  • example (RSpec::Core::Example)

    The current RSpec example.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lumberjack/capture_device/rspec.rb', line 60

def capture_logger_around_example(logger, example)
  capture_logger(logger, write_to_original: false) do |captured_device|
    example.run

    if example.exception
      logger.tag(rspec: {source_location: example.source_location, description: example.[:description]}) do
        captured_device.write_to_underlying_device
      end
    end
  end
end

#include_log_entry(expected_hash) ⇒ Lumberjack::CaptureDevice::IncludeLogEntryMatcher

Create a matcher for checking if a log entry is included in the captured logs. This matcher provides better error messages than using the include? method directly.

Examples:

expect(logs).to include_log_entry(level: :info, message: "User logged in")
expect(logs).to include_log_entry(message: /error/i, attributes: {user_id: 123})

Parameters:

  • expected_hash (Hash)

    The expected log entry attributes to match.

Options Hash (expected_hash):

  • :level (String, Symbol, Integer)

    The expected log level.

  • :severity (String, Symbol, Integer)

    Alias for :level.

  • :message (String, Regexp)

    The expected message content.

  • :attributes (Hash)

    Expected log entry attributes.

  • :tags (Hash)

    Alias for :attributes.

  • :progname (String)

    Expected program name.

Returns:



23
24
25
# File 'lib/lumberjack/capture_device/rspec.rb', line 23

def include_log_entry(expected_hash)
  Lumberjack::CaptureDevice::IncludeLogEntryMatcher.new(expected_hash)
end