Module: Logging

Included in:
Chill, Close, Config, Delete, ElasticManager, Open, Request::Elastic, SnapDelete, Snapshot, Utils
Defined in:
lib/elastic_manager/logger.rb

Overview

Universal global logging

Constant Summary collapse

SEVERITY_COLORS =
{
  'DEBUG'   => 'white',
  'INFO'    => 'green',
  'WARN'    => 'yellow',
  'ERROR'   => 'light_red',
  'FATAL'   => 'red',
  'UNKNOWN' => 'magenta'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_logger_for(classname) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/elastic_manager/logger.rb', line 39

def configure_logger_for(classname)
  logger          = Logger.new(STDOUT)
  logger.progname = classname
  logger.level    = log_level

  logger.formatter = proc do |severity, datetime, progname, msg|
    datetime = datetime.strftime('%Y-%m-%d | %I:%M:%S.%L')
    message  = "#{datetime} | #{progname} | #{severity} | #{msg}\n"
    message.send(SEVERITY_COLORS[severity])
  end

  logger
end

.log_levelObject



30
31
32
33
34
35
36
37
# File 'lib/elastic_manager/logger.rb', line 30

def log_level
  # :debug < :info < :warn < :error < :fatal < :unknown
  if ENV['LOG_LEVEL'] == '' || ENV['LOG_LEVEL'].nil?
    'INFO'
  else
    ENV['LOG_LEVEL']
  end
end

.logger_for(classname) ⇒ Object



26
27
28
# File 'lib/elastic_manager/logger.rb', line 26

def logger_for(classname)
  @loggers[classname] ||= configure_logger_for(classname)
end

Instance Method Details

#logObject



17
18
19
# File 'lib/elastic_manager/logger.rb', line 17

def log
  @log ||= Logging.logger_for(self.class.name)
end