Class: Onering::Logger

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

Class Method Summary collapse

Class Method Details

._get_level(severity) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/onering/logger.rb', line 96

def self._get_level(severity)
  case severity.to_sym
  when :fatal
    return ::Logger::FATAL
  when :error
    return ::Logger::ERROR
  when :warn
    return ::Logger::WARN
  when :info
    return ::Logger::INFO
  else
    return ::Logger::DEBUG
  end
end

.debug(message, source = nil) ⇒ Object



92
93
94
# File 'lib/onering/logger.rb', line 92

def self.debug(message, source=nil)
  self.log(:debug, message, source)
end

.error(message, source = nil) ⇒ Object



80
81
82
# File 'lib/onering/logger.rb', line 80

def self.error(message, source=nil)
  self.log(:error, message, source)
end

.fatal(message, source = nil, raise_error = false) ⇒ Object



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

def self.fatal(message, source=nil, raise_error=false)
  self.log(:fatal, message, source)
  raise Onering::Client::FatalError.new(message) if raise_error === true
end

.fatal!(message, source = nil) ⇒ Object



76
77
78
# File 'lib/onering/logger.rb', line 76

def self.fatal!(message, source=nil)
  self.fatal(message, source, true)
end

.info(message, source = nil) ⇒ Object



88
89
90
# File 'lib/onering/logger.rb', line 88

def self.info(message, source=nil)
  self.log(:info, message, source)
end

.level=(severity) ⇒ Object



57
58
59
60
# File 'lib/onering/logger.rb', line 57

def self.level=(severity)
  @_logger.sev_threshold = _get_level(severity)
  @_logger.sev_threshold
end

.log(severity, message, source = nil) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/onering/logger.rb', line 62

def self.log(severity, message, source=nil)
  if defined?(@_logger)
    @_logger.add(_get_level(severity), message, source)
    return true
  end

  return false
end

.setup(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/onering/logger.rb', line 6

def self.setup(options={})
  outputfn = options[:destination]

  if options[:destination] == /STDOUT/i
    options[:destination] = STDOUT

  elsif options[:destination] == /STDERR/i or
        options[:destination].nil?
    options[:destination] = STDERR

  elsif File.writable?(options[:destination])
    options[:destination] = File.open(options[:destination], 'a')

  else
    options[:destination] = STDERR
  end

  @_logger = ::Logger.new(options[:destination])
  @_logger.formatter = (options[:formatter] || proc{|severity, datetime, source, msg|
    case severity.downcase.to_sym
    when :fatal then sevtag = '!!'
    when :error then sevtag = 'EE'
    when :warn then sevtag = 'WW'
    when :info then sevtag = 'II'
    when :debug then sevtag = 'DD'
    else sevtag = '??'
    end

    logline = ["#{sevtag} ", (source.nil? ? nil : "[#{source}]"), msg].compact.join(' ')

    if options[:destination] === STDOUT or options[:destination] === STDERR
      case severity.downcase.to_sym
      when :fatal, :error
        logline = logline.foreground(:red)
      when :warn
        logline = logline.foreground(:yellow)
      when :info
        logline = logline.foreground(:green)
      when :debug
        logline = logline.foreground(:blue)
      end
    end

    options[:destination].puts(logline)
  })

  self.level = options[:threshold]

  self.debug("Logger is initialized. Output is #{outputfn}, threshold: #{options[:threshold]} or worse", "Onering::Logger")
end

.warn(message, source = nil) ⇒ Object



84
85
86
# File 'lib/onering/logger.rb', line 84

def self.warn(message, source=nil)
  self.log(:warn, message, source)
end