Module: ComputeUnit::Logger

Included in:
CacheStore, ComputeBase
Defined in:
lib/compute_unit/logger.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color(severity) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/compute_unit/logger.rb', line 35

def self.color(severity)
  case severity
  when ::Logger::Severity::WARN, 'WARN'
    :yellow
  when ::Logger::Severity::INFO, 'INFO'
    :green
  when ::Logger::Severity::FATAL, 'FATAL'
    :fatal
  when ::Logger::Severity::ERROR, 'ERROR'
    :fatal
  when ::Logger::Severity::DEBUG, 'DEBUG'
    :green
  else
    :green
  end
end

.log_fileObject



7
8
9
10
11
12
13
# File 'lib/compute_unit/logger.rb', line 7

def self.log_file
  if ENV['LOG_FILENAME'] && File.exist?(ENV['LOG_FILENAME'])
    ENV['LOG_FILENAME']
  else
    STDERR
  end
end

.log_levelObject



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

def self.log_level
  level = ENV['LOG_LEVEL'].downcase if ENV['LOG_LEVEL']
  case level
  when 'warn'
    ::Logger::Severity::WARN
  when 'fatal'
    ::Logger::Severity::FATAL
  when 'debug'
    ::Logger::Severity::DEBUG
  when 'info'
    ::Logger::Severity::INFO
  when 'error'
    ::Logger::Severity::ERROR
  else
    ::Logger::Severity::INFO
  end
end

.loggerObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/compute_unit/logger.rb', line 15

def self.logger
  @logger ||= begin
    log = ::Logger.new(Logger.log_file)
    log.level = log_level
    log.progname = 'ComputeUnit'
    log.formatter = proc do |severity, datetime, progname, msg|
      if Logger.log_file == STDERR
        "#{severity} - #{progname}: #{msg}\n".send(color(severity))
      else
        "#{datetime} #{severity} - #{progname}: #{msg}\n".send(color(severity))
      end
    end
    log
  end
end

Instance Method Details

#loggerObject



31
32
33
# File 'lib/compute_unit/logger.rb', line 31

def logger
  @logger ||= Logger.logger
end