Sha256: 609bc8b22ccdf7f8d1af8399a887afb15b0a27c4da4f19d6d8dcdad5f11643fc

Contents?: true

Size: 1.35 KB

Versions: 18

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.exist?(dir_path)
        FileUtils.mkdir_p(dir_path)
      end
      FileUtils.touch(file_path)
    end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
kuber_kit-1.3.8 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.7 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.1.8 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.6 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.1.7 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.5 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.4 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.1.6 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.3 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.2 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.1.5 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.1 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.3.0 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.2.7 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.2.6 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.2.5 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.2.4 lib/kuber_kit/tools/logger_factory.rb
kuber_kit-1.2.2 lib/kuber_kit/tools/logger_factory.rb