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

File, RollingFile, Stderr, Stdout, StringIo

Constant Summary

Constants included from Buffering

Buffering::DEFAULT_BUFFER_SIZE

Instance Attribute Summary

Attributes included from Buffering

#auto_flushing, #buffer

Attributes inherited from Logging::Appender

#layout, #level, #name

Instance Method Summary collapse

Methods included from Buffering

#immediate_at=

Methods inherited from Logging::Appender

#<<, #append, #closed?, #inspect, #reopen

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.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/logging/appenders/io.rb', line 17

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) rescue nil

  configure_buffering(opts)
  super(name, opts)
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.



37
38
39
40
41
42
43
44
45
# File 'lib/logging/appenders/io.rb', line 37

def close( *args )
  return self if @io.nil?
  super
  io, @io = @io, nil
  io.close unless [STDIN, STDERR, STDOUT].include?(io)
rescue IOError => err
ensure
  return self
end

#flushObject

call-seq:

flush

Call flush to force an appender to write out any buffered log events. Similar to IO#flush, so use in a similar fashion.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/logging/appenders/io.rb', line 53

def flush
  return self if @io.nil?
  @io.write(buffer.join) unless buffer.empty?
  @io.flush
  self
rescue StandardError => err
  self.level = :off
  ::Logging.log_internal {"appender #{name.inspect} has been disabled"}
  ::Logging.log_internal(-2) {err}
ensure
  buffer.clear
end