Class: SemanticLogger::Test::CaptureLogEvents

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/test/capture_log_events.rb

Overview

Logging class to captures all logging events in memory.

Example:

class UserTest < ActiveSupport::TestCase

describe User do
  let(:capture_logger) { SemanticLogger::Test::CaptureLogEvents.new }
  let(:user) { User.new }

  it "logs message" do
    user.stub(:logger, capture_logger) do
      user.enable!
    end
    assert_equal "Hello World", capture_logger.events.last.message
    assert_equal :info, capture_logger.events.last.level
  end
end

end

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #console_output?, #default_formatter, #flush, #level, #should_log?

Methods inherited from Base

#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags

Constructor Details

#initialize(level: :trace, metrics: true) ⇒ CaptureLogEvents

By default collect all log levels, and collect metric only log events.



25
26
27
28
# File 'lib/semantic_logger/test/capture_log_events.rb', line 25

def initialize(level: :trace, metrics: true)
  super(level: level, metrics: true)
  @events = []
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



22
23
24
# File 'lib/semantic_logger/test/capture_log_events.rb', line 22

def events
  @events
end

Instance Method Details

#clearObject



34
35
36
# File 'lib/semantic_logger/test/capture_log_events.rb', line 34

def clear
  @events.clear
end

#log(log) ⇒ Object



30
31
32
# File 'lib/semantic_logger/test/capture_log_events.rb', line 30

def log(log)
  @events << log
end