Class: Logging::Appenders::IO

Inherits:
Logging::Appender show all
Includes:
Buffering
Defined in:
lib/logging/appenders/io.rb

Overview

This class provides an Appender that can write to any IO stream configured for writing.

Direct Known Subclasses

Console, File, RollingFile, StringIo

Constant Summary

Constants included from Buffering

Buffering::DEFAULT_BUFFER_SIZE

Instance Attribute Summary collapse

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

Methods included from Buffering

#clear!, #flush, #flush_period?, #immediate_at=

Methods inherited from Logging::Appender

#<<, #_to_s, #add_filters, #allow, #append, #closed?, #flush, #off?, #to_s

Constructor Details

#initialize(name, io, opts = {}) ⇒ IO

call-seq:

IO.new( name, io )
IO.new( name, io, :layout => layout )

Creates a new IO Appender using the given name that will use the io stream as the logging destination.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/logging/appenders/io.rb', line 26

def initialize( name, io, opts = {} )
  unless io.respond_to? :write
    raise TypeError, "expecting an IO object but got '#{io.class.name}'"
  end

  @io = io
  @io.sync = true if io.respond_to? :sync=
  @close_method = :close

  super(name, opts)
  configure_buffering(opts)
end

Instance Attribute Details

#close_methodObject

The method that will be used to close the IO stream. Defaults to :close but can be :close_read, :close_write or nil. When nil, the IO stream will not be closed when the appender’s close method is called.



18
19
20
# File 'lib/logging/appenders/io.rb', line 18

def close_method
  @close_method
end

Instance Method Details

#close(*args) ⇒ Object

call-seq:

close( footer = true )

Close the appender and writes the layout footer to the logging destination if the footer flag is set to true. Log events will no longer be written to the logging destination after the appender is closed.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/logging/appenders/io.rb', line 46

def close( *args )
  return self if @io.nil?
  super

  io, @io = @io, nil
  if ![STDIN, STDERR, STDOUT].include?(io)
    io.send(@close_method) if @close_method && io.respond_to?(@close_method)
  end
rescue IOError
ensure
  return self
end

#reopenObject

Reopen the connection to the underlying logging destination. If the connection is currently closed then it will be opened. If the connection is currently open then it will be closed and immediately opened. If supported, the IO will have its sync mode set to ‘true` so that all writes are immediately flushed to the underlying operating system.



64
65
66
67
68
# File 'lib/logging/appenders/io.rb', line 64

def reopen
  super
  @io.sync = true if @io.respond_to? :sync=
  self
end