Module: Log

Defined in:
lib/pmux/util_logger.rb

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

IOBUF =
{}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



29
30
31
# File 'lib/pmux/util_logger.rb', line 29

def logger
  @logger
end

Class Method Details

.close(grp = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pmux/util_logger.rb', line 52

def close grp=nil
  if @logger
    @logger.close
  else
    if grp
	if (io = IOBUF[grp])
 io.close
	end
    else
	for io in IOBUF.values
 io.close
	end
    end
  end
end

.debug(*args, &block) ⇒ Object



114
115
116
# File 'lib/pmux/util_logger.rb', line 114

def debug(*args, &block)
  write :default, args.first, :DEBUG
end

.disableObject



133
134
135
136
# File 'lib/pmux/util_logger.rb', line 133

def disable
  @logger = nil
  IOBUF.clear
end

.error(*args, &block) ⇒ Object



123
124
125
# File 'lib/pmux/util_logger.rb', line 123

def error(*args, &block)
  write :default, args.first, :ERROR
end

.fatal(*args, &block) ⇒ Object



126
127
128
# File 'lib/pmux/util_logger.rb', line 126

def fatal(*args, &block)
  write :default, args.first, :FATAL
end

.info(*args, &block) ⇒ Object



117
118
119
# File 'lib/pmux/util_logger.rb', line 117

def info(*args, &block)
  write :default, args.first, :INFO
end

.init(logdev, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/pmux/util_logger.rb', line 34

def init logdev, options={}
  shift_age = options[:log_shift_age] || 8
  shift_size = options[:log_shift_size] || 100_000_000
  logger = Logger.new logdev, shift_age, shift_size
  logger.formatter = Log::Formatter.new
  logger.level = {'fatal'=>Logger::FATAL, 'error'=>Logger::ERROR,
    'warn'=>Logger::WARN, 'info'=>Logger::INFO, 'debug'=>Logger::DEBUG,
  }[(options[:log_level] || '').downcase] || Logger::INFO
  Log.logger = logger
end

.null(*args, &block) ⇒ Object



130
131
# File 'lib/pmux/util_logger.rb', line 130

def null(*args, &block)
end

.open(logdev) ⇒ Object



45
46
47
48
49
50
# File 'lib/pmux/util_logger.rb', line 45

def open logdev
  if logdev == '-'
    logdev = STDOUT
  end
  init logdev
end

.pp(*args) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/pmux/util_logger.rb', line 106

def pp(*args)
  if args.size >= 2 and args[0].kind_of?(Symbol)
    self.puts args[0], args[1..-1].map {|e| e.inspect}.join(', ')
  else
    self.puts args.map {|e| e.inspect}.join(', ')
  end
end

.puts(*args) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/pmux/util_logger.rb', line 96

def puts(*args)
  if args.size >= 2 and args[0].kind_of?(Symbol)
    grp = args.shift
  else
    grp = :default
  end
  str = args.join('').chomp + "\n"
  self.write grp, str
end

.warn(*args, &block) ⇒ Object



120
121
122
# File 'lib/pmux/util_logger.rb', line 120

def warn(*args, &block)
  write :default, args.first, :WARN
end

.write(grp, str, severity = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pmux/util_logger.rb', line 68

def write grp, str, severity=nil
  if @logger
    if severity.kind_of? Symbol
      severity = @logger.class.const_get severity
    else
      severity ||= @logger.class::INFO
    end
    begin
      logger.add severity, str.chomp
    rescue Logger::Error
      disable
    end
  else
    io = IOBUF[grp]
    if io
	if io.kind_of? File
 #mtime = io.mtime
        tstr = Time.now.strftime("%Y-%m-%d %H:%M:%S ")
        gstr = (grp == :default) ? '' : "#{grp}: "
        io.print tstr + gstr + str
      else
        io.print str
        io.print "\n" if severity #XXX
      end
    end
  end
end