|
Revision 7023, 1.2 kB
(checked in by minam, 1 year ago)
|
default the logger output to $stderr rather than STDERR (closes #8347)
|
| Line | |
|---|
| 1 |
module Capistrano |
|---|
| 2 |
class Logger |
|---|
| 3 |
attr_accessor :level |
|---|
| 4 |
attr_reader :device |
|---|
| 5 |
|
|---|
| 6 |
IMPORTANT = 0 |
|---|
| 7 |
INFO = 1 |
|---|
| 8 |
DEBUG = 2 |
|---|
| 9 |
TRACE = 3 |
|---|
| 10 |
|
|---|
| 11 |
MAX_LEVEL = 3 |
|---|
| 12 |
|
|---|
| 13 |
def initialize(options={}) |
|---|
| 14 |
output = options[:output] || $stderr |
|---|
| 15 |
if output.respond_to?(:puts) |
|---|
| 16 |
@device = output |
|---|
| 17 |
else |
|---|
| 18 |
@device = File.open(output.to_str, "a") |
|---|
| 19 |
@needs_close = true |
|---|
| 20 |
end |
|---|
| 21 |
|
|---|
| 22 |
@options = options |
|---|
| 23 |
@level = 0 |
|---|
| 24 |
end |
|---|
| 25 |
|
|---|
| 26 |
def close |
|---|
| 27 |
device.close if @needs_close |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
def log(level, message, line_prefix=nil) |
|---|
| 31 |
if level <= self.level |
|---|
| 32 |
indent = "%*s" % [MAX_LEVEL, "*" * (MAX_LEVEL - level)] |
|---|
| 33 |
message.each do |line| |
|---|
| 34 |
if line_prefix |
|---|
| 35 |
device.puts "#{indent} [#{line_prefix}] #{line.strip}\n" |
|---|
| 36 |
else |
|---|
| 37 |
device.puts "#{indent} #{line.strip}\n" |
|---|
| 38 |
end |
|---|
| 39 |
end |
|---|
| 40 |
end |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
def important(message, line_prefix=nil) |
|---|
| 44 |
log(IMPORTANT, message, line_prefix) |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def info(message, line_prefix=nil) |
|---|
| 48 |
log(INFO, message, line_prefix) |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
def debug(message, line_prefix=nil) |
|---|
| 52 |
log(DEBUG, message, line_prefix) |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def trace(message, line_prefix=nil) |
|---|
| 56 |
log(TRACE, message, line_prefix) |
|---|
| 57 |
end |
|---|
| 58 |
end |
|---|
| 59 |
end |
|---|