Sha256: 607a2e950373621681239057ff35baa9040cf4f5dcd6d9888ba5ffb066dacb84

Contents?: true

Size: 1.35 KB

Versions: 33

Compression:

Stored size: 1.35 KB

Contents

require 'logger'
require 'fileutils'

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,
  }

  MAX_LOGS_TO_KEEP  = 3
  MAX_LOG_FILE_SIZE = 512000

  include KuberKit::Import[
    "configs",
  ]

  def create(level: nil)
    prepare_log_file(configs.log_file_path)
    
    logger = Logger.new(configs.log_file_path, MAX_LOGS_TO_KEEP, MAX_LOG_FILE_SIZE)

    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::DEBUG
        "#{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

  private
    def prepare_log_file(file_path)
      dir_path = File.dirname(file_path)
      unless Dir.exists?(dir_path)
        FileUtils.mkdir_p(dir_path)
      end
      FileUtils.touch(file_path)
    end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
kuber_kit-1.1.4 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.1.2 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.2.1 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.2.0 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.1.1 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.0.1 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.0.0 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.9 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.8 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.7 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.6 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.5 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.4 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.3 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.2 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.1 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.9.0 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.8.8 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.8.7 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-0.8.6 lib/kuber_kit/tools/logger_factory.rb