class Logfoo::IoAppender

Public Class Methods

new(out: nil, err: nil, formatter: nil) click to toggle source
# File lib/logfoo/appenders/io_appender.rb, line 3
def initialize(out: nil, err: nil, formatter: nil)
  @stdout    = out || STDOUT
  @stderr    = err || STDERR
  @formatter = formatter || (tty? ? SimpleFormatter.new : LogfmtFormatter.new)

  sync!
end

Public Instance Methods

call(entry) click to toggle source
# File lib/logfoo/appenders/io_appender.rb, line 11
def call(entry)
  io =
    case entry
    when ErrLine
      @stderr
    else
      @stdout
    end

  io.write @formatter.call(entry)
  io.flush
end
sync!() click to toggle source
# File lib/logfoo/appenders/io_appender.rb, line 33
def sync!
  [@stdout, @stderr].each do |io|
    if io.respond_to?(:sync=)
      io.sync = true
    end
  end
end
tty?() click to toggle source
# File lib/logfoo/appenders/io_appender.rb, line 24
def tty?
  [@stdout, @stderr].inject(true) do |memo, io|
    if memo
      memo = io.respond_to?(:tty?) && io.tty?
    end
    memo
  end
end