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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/onering/logger.rb', line 126

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



110
111
112
# File 'lib/onering/logger.rb', line 110

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

.debug2(message, source = nil) ⇒ Object



114
115
116
# File 'lib/onering/logger.rb', line 114

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

.debug3(message, source = nil) ⇒ Object



118
119
120
# File 'lib/onering/logger.rb', line 118

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

.error(message, source = nil) ⇒ Object



98
99
100
# File 'lib/onering/logger.rb', line 98

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

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



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

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



94
95
96
# File 'lib/onering/logger.rb', line 94

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

.info(message, source = nil) ⇒ Object



106
107
108
# File 'lib/onering/logger.rb', line 106

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

.level=(severity) ⇒ Object



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

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



78
79
80
81
82
83
84
85
86
87
# File 'lib/onering/logger.rb', line 78

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



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

def self.logger()
  return @_logger
end

.output(message, source = nil) ⇒ Object



122
123
124
# File 'lib/onering/logger.rb', line 122

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

.setup(options = {}) ⇒ Object



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
58
59
60
61
62
63
# File 'lib/onering/logger.rb', line 7

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'
    when :output then sevtag = ''
    else sevtag = '??'
    end

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

    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] || :info)

  self.debug("onering-client v#{Onering::Client::VERSION} started") if defined?(Onering::FULL_CLIENT)
  self.debug("Logger is initialized. Output is #{outputfn || 'stderr'}, threshold: #{options[:threshold]} or worse", (options[:name] || "Onering::Logger"))
end

.warn(message, source = nil) ⇒ Object



102
103
104
# File 'lib/onering/logger.rb', line 102

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