Class: Lumberjack::Device::Buffer
- Inherits:
-
Device
- Object
- Device
- Lumberjack::Device::Buffer
- Defined in:
- lib/lumberjack/device/buffer.rb
Overview
A buffered logging device that wraps another logging device. Entries are buffered in memory until the buffer size is reached or the device is flushed.
Defined Under Namespace
Classes: EntryBuffer
Instance Method Summary collapse
- #buffer_size ⇒ Object
-
#buffer_size=(value) ⇒ void
Set the buffer size.
-
#close ⇒ void
Close the device.
-
#closed? ⇒ Boolean
Return true if the buffer has been closed.
-
#dev ⇒ IO
Return the underlying stream.
- #empty? ⇒ Boolean private
-
#flush ⇒ void
Flush the buffer to the underlying device.
-
#initialize(wrapped_device, options = {}) ⇒ Buffer
constructor
Initialize a new buffered logging device that wraps another device.
- #last_flushed_at ⇒ Object private
-
#reopen(logdev = nil) ⇒ Object
Reopen the underlying device, optionally with a new log destination.
-
#write(entry) ⇒ void
Write an entry to the underlying device.
Constructor Details
#initialize(wrapped_device, options = {}) ⇒ Buffer
Initialize a new buffered logging device that wraps another device.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/lumberjack/device/buffer.rb', line 140 def initialize(wrapped_device, = {}) = [:buffer_size, :flush_seconds, :before_flush] = .reject { |k, _| .include?(k) } device = Device.open_device(wrapped_device, ) @buffer = EntryBuffer.new(device, [:buffer_size] || 0, [:before_flush]) flush_seconds = [:flush_seconds] self.class.send(:create_flusher_thread, flush_seconds, @buffer) if flush_seconds.is_a?(Numeric) && flush_seconds > 0 # Add a finalizer to ensure flush is called before the object is destroyed ObjectSpace.define_finalizer(self, self.class.send(:create_finalizer, @buffer)) end |
Instance Method Details
#buffer_size ⇒ Object
154 155 156 |
# File 'lib/lumberjack/device/buffer.rb', line 154 def buffer_size @buffer.size end |
#buffer_size=(value) ⇒ void
This method returns an undefined value.
Set the buffer size. The underlying device will only be written to when the buffer size is exceeded.
163 164 165 166 |
# File 'lib/lumberjack/device/buffer.rb', line 163 def buffer_size=(value) @buffer.size = value @buffer.flush end |
#close ⇒ void
This method returns an undefined value.
Close the device.
179 180 181 182 183 184 185 |
# File 'lib/lumberjack/device/buffer.rb', line 179 def close @buffer.close @buffer.device.close # Remove the finalizer since we've already flushed ObjectSpace.undefine_finalizer(self) end |
#closed? ⇒ Boolean
Return true if the buffer has been closed.
188 189 190 |
# File 'lib/lumberjack/device/buffer.rb', line 188 def closed? @buffer.closed? end |
#dev ⇒ IO
Return the underlying stream. Provided for API compatibility with Logger devices.
210 211 212 |
# File 'lib/lumberjack/device/buffer.rb', line 210 def dev @buffer.device.dev end |
#empty? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
220 221 222 |
# File 'lib/lumberjack/device/buffer.rb', line 220 def empty? @buffer.empty? end |
#flush ⇒ void
This method returns an undefined value.
Flush the buffer to the underlying device.
195 196 197 |
# File 'lib/lumberjack/device/buffer.rb', line 195 def flush @buffer.flush end |
#last_flushed_at ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
215 216 217 |
# File 'lib/lumberjack/device/buffer.rb', line 215 def last_flushed_at @buffer.last_flushed_at end |
#reopen(logdev = nil) ⇒ Object
Reopen the underlying device, optionally with a new log destination.
200 201 202 203 204 205 |
# File 'lib/lumberjack/device/buffer.rb', line 200 def reopen(logdev = nil) flush @buffer.device.reopen(logdev) @buffer.reopen ObjectSpace.define_finalizer(self, self.class.send(:create_finalizer, @buffer)) end |
#write(entry) ⇒ void
This method returns an undefined value.
Write an entry to the underlying device.
172 173 174 |
# File 'lib/lumberjack/device/buffer.rb', line 172 def write(entry) @buffer << entry end |