Sha256: 19076724c637bac10ee4d75ede66986d24b3cd242e43dd0f255bd0b276827a11

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'fileutils'
require 'logger'

module Entangler
  class Logger
    def self.create_log_dir(base_dir)
      FileUtils.mkdir_p(File.dirname(log_file_path(base_dir)))
    end

    def self.log_file_path(base_dir, log_file_name = 'entangler.log')
      File.join(base_dir, '.entangler', 'log', log_file_name)
    end

    def initialize(outputs, verbose = false)
      @loggers = Array(outputs).map do |output|
        logger = ::Logger.new(output)

        logger.level = verbose ? ::Logger::DEBUG : ::Logger::INFO
        logger.formatter = proc do |severity, datetime, _, msg|
          date_format = datetime.strftime('%Y-%m-%d %H:%M:%S')
          "[#{date_format}] #{severity.rjust(5)}: #{msg}\n"
        end

        logger
      end
    end

    def level=(level)
      @loggers.each { |logger| logger.level = level }
    end

    ::Logger::Severity.constants.each do |level|
      define_method(level.downcase) do |*args|
        @loggers.each { |logger| logger.send(level.downcase, *args) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
entangler-1.1.2 lib/entangler/logger.rb