Class: Logging::Appenders::StringIo
- Inherits:
-
IO
- Object
- Logging::Appender
- IO
- Logging::Appenders::StringIo
- Defined in:
- lib/logging/appenders/string_io.rb
Overview
This class provides an Appender that can write to a StringIO instance. This is very useful for testing log message output.
Defined Under Namespace
Modules: IoToS
Constant Summary
Constants included from Buffering
Buffering::DEFAULT_BUFFER_SIZE
Instance Attribute Summary collapse
-
#sio ⇒ Object
readonly
The StringIO instance the appender is writing to.
Attributes inherited from IO
Attributes included from Buffering
#async, #auto_flushing, #buffer, #flush_period, #write_size
Attributes inherited from Logging::Appender
#encoding, #filters, #layout, #level, #name
Instance Method Summary collapse
-
#clear ⇒ Object
(also: #reset)
Clears the internal StringIO instance.
-
#initialize(name, opts = {}) ⇒ StringIo
constructor
call-seq: StringIo.new( name, opts = {} ).
-
#reopen ⇒ Object
Reopen the underlying StringIO instance.
Methods inherited from IO
Methods included from Buffering
#clear!, #close, #flush, #flush_period?, #immediate_at=
Methods inherited from Logging::Appender
#<<, #_to_s, #add_filters, #allow, #append, #close, #closed?, #flush, #off?, #to_s
Constructor Details
#initialize(name, opts = {}) ⇒ StringIo
call-seq:
StringIo.new( name, opts = {} )
Creates a new StringIo appender that will append log messages to a StringIO instance.
25 26 27 28 29 30 |
# File 'lib/logging/appenders/string_io.rb', line 25 def initialize( name, opts = {} ) @sio = StringIO.new @sio.extend IoToS @pos = 0 super(name, @sio, opts) end |
Instance Attribute Details
#sio ⇒ Object (readonly)
The StringIO instance the appender is writing to.
17 18 19 |
# File 'lib/logging/appenders/string_io.rb', line 17 def sio @sio end |
Instance Method Details
#clear ⇒ Object Also known as: reset
Clears the internal StringIO instance. All log messages are removed from the buffer.
53 54 55 56 57 58 59 |
# File 'lib/logging/appenders/string_io.rb', line 53 def clear @mutex.synchronize { @pos = 0 @sio.seek 0 @sio.truncate 0 } end |
#reopen ⇒ Object
Reopen the underlying StringIO instance. If the instance is currently closed then it will be opened. If the instance is currently open then it will be closed and immediately opened.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/logging/appenders/string_io.rb', line 36 def reopen @mutex.synchronize { if defined? @io and @io flush @io.close rescue nil end @io = @sio = StringIO.new @sio.extend IoToS @pos = 0 } super self end |