Class: SemanticLogger::SyncProcessor
- Inherits:
-
Object
- Object
- SemanticLogger::SyncProcessor
- 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.
Ruby does not allow a Monitor to be entered in a signal trap, so another thread runs a call made in a trap. A log entry is left on a queue of pending calls, which whoever next enters the monitor runs first, in order, and a thread started from the trap enters it to do so when nothing else does (see #defer). Every other call runs on a thread that the trap starts and waits for, at most TRAP_WAIT_SECONDS (see #outside_trap). Neither ever runs on the trap's thread, so that an appender can lock a Mutex of its own as it writes, flushes, closes, or reopens, as a Ruby Logger does.
Constant Summary collapse
- TRAP_WAIT_SECONDS =
Seconds that a call other than a log call, such as a flush or a reopen, waits in a signal trap for the thread that runs it. It may need a lock that the thread the trap interrupted holds.
1
Instance Attribute Summary collapse
-
#appenders ⇒ Object
readonly
Returns the value of attribute appenders.
Class Method Summary collapse
-
.logger ⇒ Object
Internal logger for SemanticLogger For example when an appender is not working etc..
-
.logger=(logger) ⇒ Object
Allow the internal logger to be overridden from its default of $stderr Can be replaced with another Ruby logger or Rails logger, but never to SemanticLogger::Logger itself since it is for reporting problems while trying to log to the various appenders (raises ArgumentError).
Instance Method Summary collapse
-
#add(**args, &block) ⇒ Object
Add an appender.
-
#close ⇒ Object
Close the appenders, as for #flush in a signal trap.
-
#current_thread_inside? ⇒ Boolean
Returns [true|false] whether the calling thread is inside a call to this processor, such as in a signal trap that interrupted a log call.
-
#flush ⇒ Object
Flush the appenders.
-
#initialize(appenders = nil) ⇒ SyncProcessor
constructor
A new instance of SyncProcessor.
-
#log(log) ⇒ Object
Logs on the calling thread, which belongs to the application, so a stack overflow while writing (see Appenders#log) is reported rather than raised from the log call.
-
#logger ⇒ Object
Returns the internal logger of the appenders, for reporting a problem while logging, such as an exception raised by an on_log subscriber.
-
#queue_size ⇒ Object
Returns [Integer] the number of entries logged in a signal trap that are still to be written (see #log), the only ones that wait in synchronous mode.
-
#remove(appender) ⇒ Object
Remove the appender from those that this processor writes to, and close it, once any log call writing to it has returned.
-
#reopen(*args) ⇒ Object
Reopen the appenders on the calling thread, or, in a signal trap, as for #flush, so that an appender that connects or locks a Mutex as it reopens can do so.
- #start ⇒ Object
-
#stats ⇒ Object
Returns [Hash] operational statistics for the logging pipeline.
Constructor Details
#initialize(appenders = nil) ⇒ SyncProcessor
Returns a new instance of SyncProcessor.
141 142 143 144 145 146 147 148 |
# File 'lib/semantic_logger/sync_processor.rb', line 141 def initialize(appenders = nil) @monitor = Monitor.new @appenders = appenders || Appenders.new(self.class.logger.dup) @processed_count = 0 @pending = Thread::Queue.new @runner = nil @pid = Process.pid end |
Instance Attribute Details
#appenders ⇒ Object (readonly)
Returns the value of attribute appenders.
133 134 135 |
# File 'lib/semantic_logger/sync_processor.rb', line 133 def appenders @appenders end |
Class Method Details
.logger ⇒ Object
Internal logger for SemanticLogger For example when an appender is not working etc.. By default logs to $stderr
124 125 126 127 128 129 130 131 |
# File 'lib/semantic_logger/sync_processor.rb', line 124 def self.logger @logger ||= begin l = SemanticLogger::Appender::IO.new($stderr, level: :warn) l.name = name l end end |
.logger=(logger) ⇒ Object
Allow the internal logger to be overridden from its default of $stderr Can be replaced with another Ruby logger or Rails logger, but never to SemanticLogger::Logger itself since it is for reporting problems while trying to log to the various appenders (raises ArgumentError)
A Ruby or Rails logger is wrapped in an appender (see Appender.internal_logger), so .logger returns that appender.
117 118 119 |
# File 'lib/semantic_logger/sync_processor.rb', line 117 def self.logger=(logger) @logger = Appender.internal_logger(logger) end |
Instance Method Details
#add(**args, &block) ⇒ Object
Add an appender. In a signal trap, as for #flush, which returns nil instead of the appender when it is not added in time. SemanticLogger.add_appender adds to the appenders directly instead.
24 25 26 |
# File 'lib/semantic_logger/sync_processor.rb', line 24 def add(**args, &block) synchronize { @appenders.add(**args, &block) } end |
#close ⇒ Object
Close the appenders, as for #flush in a signal trap.
A stack overflow while closing is reported rather than raised, as by #log, since the calling thread belongs to the application, even when Appenders#close raises it, for the asynchronous Processor to move to a new thread.
68 69 70 71 72 73 74 75 |
# File 'lib/semantic_logger/sync_processor.rb', line 68 def close synchronize do @appenders.close rescue SystemStackError => e logger.error("Failed to close the appenders: the stack overflowed", e) nil end end |
#current_thread_inside? ⇒ Boolean
Returns [true|false] whether the calling thread is inside a call to this processor, such as in a signal trap that interrupted a log call. Another thread then waits for that call to return before it can use this processor, so the calling thread must not wait for one that does.
106 107 108 |
# File 'lib/semantic_logger/sync_processor.rb', line 106 def current_thread_inside? @monitor.mon_owned? end |
#flush ⇒ Object
Flush the appenders. In a signal trap, a thread that the trap starts flushes them, after the entries logged in the trap before, and the trap waits for it, at most TRAP_WAIT_SECONDS, then returns its result, or raises what it raised. When it has not finished by then, such as when the trap interrupted a call inside this processor, which cannot return before the trap does, it returns nil and the flush runs just after.
59 60 61 |
# File 'lib/semantic_logger/sync_processor.rb', line 59 def flush synchronize { @appenders.flush } end |
#log(log) ⇒ Object
Logs on the calling thread, which belongs to the application, so a stack overflow while writing (see Appenders#log) is reported rather than raised from the log call.
In a signal trap, the entry is written just after, by another thread, before any entry logged after the trap, and the trap returns without waiting for it (see #defer).
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/semantic_logger/sync_processor.rb', line 33 def log(log) logged = exclusively { write(log) } return logged unless logged.equal?(TRAPPED) defer { write(log) } true rescue SystemStackError => e @appenders.logger.error("Failed to log a message: the stack overflowed", e) false end |
#logger ⇒ Object
Returns the internal logger of the appenders, for reporting a problem while logging, such as an exception raised by an on_log subscriber. The same logger as the asynchronous Processor's.
137 138 139 |
# File 'lib/semantic_logger/sync_processor.rb', line 137 def logger @appenders.logger end |
#queue_size ⇒ Object
Returns [Integer] the number of entries logged in a signal trap that are still to be written (see #log), the only ones that wait in synchronous mode. Never enters the monitor.
99 100 101 |
# File 'lib/semantic_logger/sync_processor.rb', line 99 def queue_size pending_count end |
#remove(appender) ⇒ Object
Remove the appender from those that this processor writes to, and close it, once any log call writing to it has returned. Closes it after leaving the monitor, so that the other threads can log meanwhile. Returns the result of closing it, or, in a signal trap, as for #flush, nil when it is not closed in time.
The appenders are reopened on the calling thread (see #reopen), so none is ever waiting to be reopened when it is removed.
84 85 86 87 88 89 |
# File 'lib/semantic_logger/sync_processor.rb', line 84 def remove(appender) removed = exclusively { @appenders.flush if @appenders.delete(appender) } return outside_trap { remove(appender) } if removed.equal?(TRAPPED) appender.close end |
#reopen(*args) ⇒ Object
Reopen the appenders on the calling thread, or, in a signal trap, as for #flush, so that an appender that connects or locks a Mutex as it reopens can do so.
93 94 95 |
# File 'lib/semantic_logger/sync_processor.rb', line 93 def reopen(*args) synchronize { @appenders.reopen(*args) } end |
#start ⇒ Object
150 151 152 |
# File 'lib/semantic_logger/sync_processor.rb', line 150 def start # NOP end |
#stats ⇒ Object
Returns [Hash] operational statistics for the logging pipeline.
In synchronous mode messages are written inline on the calling thread, so the queue holds only the entries logged in a signal trap that are still to be written (see #log), and no messages are dropped. In a trap it reads them without entering the monitor.
49 50 51 52 |
# File 'lib/semantic_logger/sync_processor.rb', line 49 def stats stats = exclusively { statistics } stats.equal?(TRAPPED) ? statistics : stats end |