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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/onering/logger.rb', line 116

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



104
105
106
# File 'lib/onering/logger.rb', line 104

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

.debug2(message, source = nil) ⇒ Object



108
109
110
# File 'lib/onering/logger.rb', line 108

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

.debug3(message, source = nil) ⇒ Object



112
113
114
# File 'lib/onering/logger.rb', line 112

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

.error(message, source = nil) ⇒ Object



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

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

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



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

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



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

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

.info(message, source = nil) ⇒ Object



100
101
102
# File 'lib/onering/logger.rb', line 100

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

.level=(severity) ⇒ Object



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

def self.level=(severity)
  @_logger.sev_threshold = _get_level(severity)
  @_verbosity = (Integer(severity.to_s[-1]) rescue 1)

  @_logger.sev_threshold
end

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



72
73
74
75
76
77
78
79
80
81
# File 'lib/onering/logger.rb', line 72

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

  return false
end

.loggerObject



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

def self.logger()
  return @_logger
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
56
57
# File 'lib/onering/logger.rb', line 6

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

  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, :debug2, :debug3
        logline = logline.foreground(:blue)
      end
    end

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

  self.level = options[:threshold]

  self.debug("onering-client v#{Onering::Client::VERSION} started")
  self.debug("Logger is initialized. Output is #{outputfn}, threshold: #{options[:threshold]} or worse", "Onering::Logger")
end

.warn(message, source = nil) ⇒ Object



96
97
98
# File 'lib/onering/logger.rb', line 96

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