Class: SemanticLogger::Test::CaptureLogEvents
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- SemanticLogger::Test::CaptureLogEvents
- 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.
assert_equal :info, log.level
end
end
end
Constant Summary
Constants inherited from Subscriber
Instance Attribute Summary collapse
-
#events ⇒ Object
Returns the value of attribute events.
Attributes inherited from Subscriber
#application, #environment, #formatter, #host, #logger, #metrics
Instance Method Summary collapse
-
#batch(logs) ⇒ Object
Supports batching of log events.
- #clear ⇒ Object
-
#current_thread_inside? ⇒ Boolean
Returns [false]: standing in for the processor, it holds no lock, so another thread never waits for the calling thread to return from a call to it.
-
#initialize(level: :trace, metrics: true) ⇒ CaptureLogEvents
constructor
By default collect all log levels, and collect metric only log events.
-
#log(log) ⇒ Object
Standing in for a logger, it runs the
on_logsubscribers for the log events it created, as a logger does. - #to_h ⇒ Object
Methods inherited from Subscriber
#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #close, #console_output?, #console_stream, #default_formatter, #fails_any_batch?, #flush, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?
Constructor Details
#initialize(level: :trace, metrics: true) ⇒ CaptureLogEvents
By default collect all log levels, and collect metric only log events.
26 27 28 29 30 |
# File 'lib/semantic_logger/test/capture_log_events.rb', line 26 def initialize(level: :trace, metrics: true) super @events = [] @created = ObjectSpace::WeakMap.new end |
Instance Attribute Details
#events ⇒ Object
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
42 43 44 |
# File 'lib/semantic_logger/test/capture_log_events.rb', line 42 def batch(logs) logs.each { |log| self.log(log) } end |
#clear ⇒ Object
46 47 48 |
# File 'lib/semantic_logger/test/capture_log_events.rb', line 46 def clear @events.clear end |
#current_thread_inside? ⇒ Boolean
Returns [false]: standing in for the processor, it holds no lock, so another thread never waits for the calling thread to return from a call to it.
52 53 54 |
# File 'lib/semantic_logger/test/capture_log_events.rb', line 52 def current_thread_inside? false end |
#log(log) ⇒ Object
Standing in for a logger, it runs the on_log subscribers for the log events it created, as a
logger does. Log events from a logger, received as the processor or an appender, already
went through them. A log event built elsewhere and passed to #log is captured without
running them, since it cannot be told apart from one received from a logger.
36 37 38 39 |
# File 'lib/semantic_logger/test/capture_log_events.rb', line 36 def log(log) Logger.call_subscribers(log) if @created.key?(log) @events << log end |
#to_h ⇒ Object
56 57 58 |
# File 'lib/semantic_logger/test/capture_log_events.rb', line 56 def to_h events.map(&:to_h) end |