Class: SemanticLogger::SyncProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/sync_processor.rb

Overview

The SyncProcessor performs logging in the current thread.

Appenders are designed to only be used by one thread at a time, so all calls are monitor protected in case SyncProcessor is being used in a multi-threaded environment.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appenders = nil) ⇒ SyncProcessor

Returns a new instance of SyncProcessor.



70
71
72
73
74
# File 'lib/semantic_logger/sync_processor.rb', line 70

def initialize(appenders = nil)
  @monitor         = Monitor.new
  @appenders       = appenders || Appenders.new(self.class.logger.dup)
  @processed_count = 0
end

Class Attribute Details

.loggerObject

Internal logger for SemanticLogger For example when an appender is not working etc.. By default logs to $stderr



59
60
61
62
63
64
65
66
# File 'lib/semantic_logger/sync_processor.rb', line 59

def self.logger
  @logger ||=
    begin
      l      = SemanticLogger::Appender::IO.new($stderr, level: :warn)
      l.name = name
      l
    end
end

Instance Attribute Details

#appendersObject (readonly)

Returns the value of attribute appenders.



68
69
70
# File 'lib/semantic_logger/sync_processor.rb', line 68

def appenders
  @appenders
end

Instance Method Details

#addObject



7
8
9
# File 'lib/semantic_logger/sync_processor.rb', line 7

def add(...)
  @monitor.synchronize { @appenders.add(...) }
end

#closeObject



40
41
42
# File 'lib/semantic_logger/sync_processor.rb', line 40

def close
  @monitor.synchronize { @appenders.close }
end

#flushObject



36
37
38
# File 'lib/semantic_logger/sync_processor.rb', line 36

def flush
  @monitor.synchronize { @appenders.flush }
end

#logObject



11
12
13
14
15
16
# File 'lib/semantic_logger/sync_processor.rb', line 11

def log(...)
  @monitor.synchronize do
    @processed_count += 1
    @appenders.log(...)
  end
end

#reopen(*args) ⇒ Object



44
45
46
# File 'lib/semantic_logger/sync_processor.rb', line 44

def reopen(*args)
  @monitor.synchronize { @appenders.reopen(*args) }
end

#startObject



76
77
78
# File 'lib/semantic_logger/sync_processor.rb', line 76

def start
  # NOP
end

#statsObject

Returns [Hash] operational statistics for the logging pipeline.

In synchronous mode there is no queue: messages are written inline on the calling thread, so queue_size is always 0 and no messages can be dropped.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/semantic_logger/sync_processor.rb', line 22

def stats
  @monitor.synchronize do
    {
      queue_size:     0,
      capped:         false,
      max_queue_size: nil,
      thread_active:  false,
      processed:      @processed_count,
      dropped:        0,
      appenders:      @appenders.stats
    }
  end
end