Class: Fate::MultiLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/fate/logger.rb

Overview

Simple logging class designed to interleave the output from multiple processes while making it obvious which lines were logged by which process.

Defined Under Namespace

Classes: Sublogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MultiLogger

Returns a new instance of MultiLogger.



9
10
11
12
13
14
15
16
# File 'lib/fate/logger.rb', line 9

def initialize(options)
  @width = options[:width]
  if file = options[:file]
    @io = File.new(file, "a")
  elsif io = options[:io]
    @io = io
  end
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



8
9
10
# File 'lib/fate/logger.rb', line 8

def io
  @io
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/fate/logger.rb', line 8

def width
  @width
end

Instance Method Details

#[](name) ⇒ Object



18
19
20
# File 'lib/fate/logger.rb', line 18

def [](name)
  Sublogger.new(self, name)
end

#colorize(name, string) ⇒ Object



86
87
88
# File 'lib/fate/logger.rb', line 86

def colorize(name, string)
  [Term::ANSIColor.send(name), string, Term::ANSIColor.reset].join
end

#debug(name, string) ⇒ Object



72
73
74
# File 'lib/fate/logger.rb', line 72

def debug(name, string)
  write(name, string)
end

#error(name, string) ⇒ Object



60
61
62
# File 'lib/fate/logger.rb', line 60

def error(name, string)
  write(name, string, "red")
end

#format(name, string) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/fate/logger.rb', line 76

def format(name, string)
  string.chomp!
  if name == @last_identifier
    "%-#{width}s - %s\n" % [nil, string]
  else
    @last_identifier = name
    "%-#{width}s - %s\n" % [name, string]
  end
end

#green(name, string) ⇒ Object



64
65
66
# File 'lib/fate/logger.rb', line 64

def green(name, string)
  write(name, string, "green")
end

#info(name, string) ⇒ Object



68
69
70
# File 'lib/fate/logger.rb', line 68

def info(name, string)
  write(name, string, "yellow")
end

#write(name, string, color = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/fate/logger.rb', line 49

def write(name, string, color=nil)
  if color
    line = colorize(color, format(name, string))
  else
    line = format(name, string)
  end
  num = @io.write line
  @io.flush
  num
end