Sha256: 54a5fe0386119987bdc0ac594df9058fb177e22b15b76b23d6b190095e4078b2

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

require 'logger'

class KuberKit::Tools::LoggerFactory
  SEVERITY_COLORS_BY_LEVEL = {
    Logger::INFO   => String::Colors::GREEN,
    Logger::WARN   => String::Colors::PURPLE,
    Logger::DEBUG  => String::Colors::YELLOW,
    Logger::ERROR  => String::Colors::RED,
    Logger::FATAL  => String::Colors::PURPLE,
  }

  include KuberKit::Import[
    "configs",
  ]

  def create(stdout = nil, level = nil)
    logger = Logger.new(stdout || configs.log_file_path)

    logger.level = level || Logger::DEBUG

    logger.formatter = proc do |severity, datetime, progname, msg|
      level = Logger::SEV_LABEL.index(severity)

      severity_color = SEVERITY_COLORS_BY_LEVEL[level]

      severity_text  = severity.to_s
      severity_text  = severity_text.colorize(severity_color) if severity_color

      if level == Logger::INFO
        "#{datetime.strftime("%Y/%m/%d %H:%M:%S").grey} #{msg}\n"
      else
        "#{datetime.strftime("%Y/%m/%d %H:%M:%S").grey} #{severity_text.downcase}: #{msg}\n"
      end
    end

    logger
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kuber_kit-0.3.11 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.3.10 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.3.9 lib/kuber_kit/tools/logger_factory.rb