Class: Lumberjack::Device::Buffer
- Inherits:
-
Lumberjack::Device
- Object
- Lumberjack::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.
Methods inherited from Lumberjack::Device
#datetime_format, #datetime_format=, open_device
Constructor Details
#initialize(wrapped_device, options = {}) ⇒ Buffer
Initialize a new buffered logging device that wraps another device.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lumberjack/device/buffer.rb', line 114 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
128 129 130 |
# File 'lib/lumberjack/device/buffer.rb', line 128 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.
137 138 139 140 |
# File 'lib/lumberjack/device/buffer.rb', line 137 def buffer_size=(value) @buffer.size = value @buffer.flush end |
#close ⇒ void
This method returns an undefined value.
Close the device.
153 154 155 156 157 158 159 |
# File 'lib/lumberjack/device/buffer.rb', line 153 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.
162 163 164 |
# File 'lib/lumberjack/device/buffer.rb', line 162 def closed? @buffer.closed? end |
#dev ⇒ IO
Return the underlying stream. Provided for API compatibility with Logger devices.
184 185 186 |
# File 'lib/lumberjack/device/buffer.rb', line 184 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.
194 195 196 |
# File 'lib/lumberjack/device/buffer.rb', line 194 def empty? @buffer.empty? end |
#flush ⇒ void
This method returns an undefined value.
Flush the buffer to the underlying device.
169 170 171 |
# File 'lib/lumberjack/device/buffer.rb', line 169 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.
189 190 191 |
# File 'lib/lumberjack/device/buffer.rb', line 189 def last_flushed_at @buffer.last_flushed_at end |
#reopen(logdev = nil) ⇒ Object
Reopen the underlying device, optionally with a new log destination.
174 175 176 177 178 179 |
# File 'lib/lumberjack/device/buffer.rb', line 174 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.
146 147 148 |
# File 'lib/lumberjack/device/buffer.rb', line 146 def write(entry) @buffer << entry end |