Class: Cabin::Outputs::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/cabin/outputs/io.rb

Overview

Wrap IO objects with a reasonable log output.

If the IO is not attached to a tty (io#tty? returns false), then the event will be written in ruby inspect format terminated by a newline:

{ "timestamp" => ..., "message" => message, ... }

If the IO is attached to a TTY, there are # human-friendly in this format:

message { event data }

Additionally, colorized output may be available. If the event has :level, :color, or :bold. Any of the Cabin::Mixins::Logger methods (info, error, etc) will result in colored output. See the LEVELMAP for the mapping of levels to colors.

Constant Summary collapse

CODEMAP =

Mapping of color/style names to ANSI control values

{
  :normal => 0,
  :bold => 1,
  :black => 30,
  :red => 31,
  :green => 32,
  :yellow => 33,
  :blue => 34,
  :magenta => 35,
  :cyan => 36,
  :white => 37
}
LEVELMAP =

Map of log levels to colors

{
  :fatal => :red,
  :error => :red,
  :warn => :yellow,
  :info => :green, # default color
  :debug => :cyan,
}

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ IO

Returns a new instance of IO.



43
44
45
46
# File 'lib/cabin/outputs/io.rb', line 43

def initialize(io)
  @io = io
  @lock = Mutex.new
end

Instance Method Details

#<<(event) ⇒ Object

Receive an event



49
50
51
52
53
54
55
56
57
58
# File 'lib/cabin/outputs/io.rb', line 49

def <<(event)
  @lock.synchronize do
    if !tty?
      @io.puts(event.inspect)
      @io.flush
    else
      tty_write(event)
    end
  end
end

#tty?Boolean

def <<

Returns:

  • (Boolean)


60
61
62
# File 'lib/cabin/outputs/io.rb', line 60

def tty?
  @io.tty?
end