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(:logger) { SemanticLogger::Test::CaptureLogEvents.new }
    let(:user) { User.new }

    it "logs message" do
      user.stub(:logger, logger) do
        user.enable!
      end
      assert log = logger.events.first
      assert_equal "Hello World", log.message
      assert_equal :info, log.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.



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

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

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



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

def events
  @events
end

Instance Method Details

#batch(logs) ⇒ Object

Supports batching of log events



37
38
39
# File 'lib/semantic_logger/test/capture_log_events.rb', line 37

def batch(logs)
  @events += log
end

#clearObject



41
42
43
# File 'lib/semantic_logger/test/capture_log_events.rb', line 41

def clear
  @events.clear
end

#level_indexObject

Support silencing of log messages



46
47
48
# File 'lib/semantic_logger/test/capture_log_events.rb', line 46

def level_index
  @level_index || SemanticLogger.default_level_index
end

#log(log) ⇒ Object



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

def log(log)
  Logger.call_subscribers(log)
  @events << log
end

#to_hObject



50
51
52
# File 'lib/semantic_logger/test/capture_log_events.rb', line 50

def to_h
  events.map(&:to_h)
end