Module: Gemstash::Logging

Overview

:nodoc:

Defined Under Namespace

Classes: RackMiddleware, StreamLogger

Constant Summary collapse

LEVELS =
{
  debug: Logger::DEBUG,
  info: Logger::INFO,
  warn: Logger::WARN,
  error: Logger::ERROR,
  fatal: Logger::FATAL
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loggerObject



39
40
41
# File 'lib/gemstash/logging.rb', line 39

def self.logger
  @logger ||= setup_logger($stdout)
end

.resetObject



43
44
45
46
# File 'lib/gemstash/logging.rb', line 43

def self.reset
  @logger.close if @logger
  @logger = nil
end

.setup_logger(logfile) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gemstash/logging.rb', line 25

def self.setup_logger(logfile)
  @logger = Logger.new(logfile, 2, 10_485_760)
  @logger.level = Logger::INFO
  @logger.datetime_format = "%d/%b/%Y:%H:%M:%S %z"
  @logger.formatter = proc do |severity, datetime, _progname, msg|
    if msg.end_with?("\n")
      "[#{datetime}] - #{severity} - #{msg}"
    else
      "[#{datetime}] - #{severity} - #{msg}\n"
    end
  end
  @logger
end

Instance Method Details

#logObject



15
16
17
# File 'lib/gemstash/logging.rb', line 15

def log
  Gemstash::Logging.logger
end

#log_error(message, error, level: :error) ⇒ Object



19
20
21
22
23
# File 'lib/gemstash/logging.rb', line 19

def log_error(message, error, level: :error)
  log.add(LEVELS[level]) do
    "#{message} - #{error.message} (#{error.class})\n  #{error.backtrace.join("\n  ")}"
  end
end