Files
dtic-DIIAA/dtic-BKPs/lib/logger.rb
T

76 lines
1.7 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# frozen_string_literal: true
# ==========================================================
# dtic-BKPs/lib/logger.rb — Logger con colores y rotación
# ==========================================================
require 'logger'
require 'fileutils'
module DTICBKPs
# Colores ANSI
module C
RESET = "\e[0m"
BOLD = "\e[1m"
DIM = "\e[2m"
RED = "\e[91m"
GREEN = "\e[92m"
YELLOW = "\e[93m"
CYAN = "\e[96m"
end
# Iconos semánticos
OK = "#{C::GREEN}#{C::RESET}"
ERR = "#{C::RED}#{C::RESET}"
WARN = "#{C::YELLOW}#{C::RESET}"
INFO = "#{C::CYAN}#{C::RESET}"
class AppLogger
def initialize(log_dir)
FileUtils.mkdir_p(log_dir)
log_path = File.join(log_dir, 'dtic-BKPs.log')
@file_logger = Logger.new(log_path, 'daily')
@file_logger.level = Logger::INFO
@file_logger.formatter = proc { |sev, dt, _, msg|
"[#{dt.strftime('%Y-%m-%d %H:%M:%S')}] [#{sev}] #{msg}\n"
}
end
def info(msg)
@file_logger.info(strip_ansi(msg))
puts "#{INFO} #{msg}"
end
def warn(msg)
@file_logger.warn(strip_ansi(msg))
puts "#{WARN} #{msg}"
end
def error(msg)
@file_logger.error(strip_ansi(msg))
puts "#{ERR} #{msg}"
end
def ok(msg)
@file_logger.info(strip_ansi(msg))
puts "#{OK} #{msg}"
end
def titulo(msg)
@file_logger.info(strip_ansi(msg))
puts "\n#{C::BOLD}#{C::CYAN}--- #{msg} ---#{C::RESET}"
end
def paso(msg)
@file_logger.info(strip_ansi(msg))
puts "#{C::CYAN}#{msg}#{C::RESET}"
end
private
def strip_ansi(s)
s.to_s.gsub(/\e\[[\d;]*[mK]/, '')
end
end
end