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
17
# File 'lib/fate/logger.rb', line 9

def initialize(options)
  @disable_color = options[:disable_color]
  @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



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

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

#colorize(name, string) ⇒ Object



91
92
93
# File 'lib/fate/logger.rb', line 91

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

#debug(name, string) ⇒ Object



77
78
79
# File 'lib/fate/logger.rb', line 77

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

#error(name, string) ⇒ Object



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

def error(name, string)
  write(name, "ERROR: #{string}", "red")
end

#format(name, string) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/fate/logger.rb', line 81

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



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

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

#info(name, string) ⇒ Object



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

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

#warn(name, string) ⇒ Object



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

def warn(name, string)
  write(name, "WARN: #{string}", "magenta")
end

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



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

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