# frozen_string_literal: true require "logger" require "colorize" require_relative "symbols" # This module provides classes for the Makit gem. module Makit module Logging ANSI_COLOR_REGEX = /\e\[[0-9;]*m/ class PlainFormatter < Logger::Formatter def call(_severity, _time, _progname, msg) stripped_msg = msg.gsub(ANSI_COLOR_REGEX, "") # Remove ANSI color codes "#{stripped_msg}\n" end end class ColorFormatter < Logger::Formatter def call(_severity, _time, _progname, msg) "#{msg}\n" end end # This class provide methods for working with Directories/ # # Example: # # Makit::Directory.find_directory_with_pattern("/home/user", "*.rb") # class MultiLogger def initialize(*targets) @targets = targets end def add(severity, message = nil, progname = nil, &block) @targets.each do |logger| logger.add(severity, message, progname, &block) logger.flush if logger.respond_to?(:flush) end end def <<(message) @targets.each do |logger| logger << message logger.flush if logger.respond_to?(:flush) end end def close @targets.each(&:close) end def method_missing(method, *args, &block) @targets.each { |logger| logger.send(method, *args, &block) } end def respond_to_missing?(method, include_private = false) @targets.all? { |logger| logger.respond_to?(method, include_private) } end def self.create_logger stdout_logger = Logger.new($stdout) # ColoredLogger.new(STDOUT) stdout_logger.level = Logger::DEBUG # Assign the custom formatter to the file_logger stdout_logger.formatter = ColorFormatter.new # if clean or clobber commands are used, then log ONLY to stdout if ARGV.include?("clean") || ARGV.include?("clobber") return stdout_logger end if Makit::Environment.project_root_directory.nil? logger = stdout_logger else #log_filename = if ARGV.empty? # "#{Makit::Environment.project_root_directory}/artifacts/rake.log" # else # "#{Makit::Environment.project_root_directory}/artifacts/rake_#{ARGV.join("_").gsub(":", # "_")}.log" # end # FileUtils.remove_file(log_file) if File.exist?(log_file) #FileUtils.mkdir_p(File.dirname(log_filename)) unless Dir.exist?(File.dirname(log_filename)) #File.open(log_filename, "w") #file_logger = Logger.new(log_filename) #file_logger.level = Logger::DEBUG # Assign the custom formatter to the file_logger #file_logger.formatter = PlainFormatter.new #logger = MultiLogger.new(file_logger, stdout_logger) logger = stdout_logger end logger end end end end