lib/framework/logging/logger.rb in facter-4.0.8.pre vs lib/framework/logging/logger.rb in facter-4.0.9.pre

- old
+ new

@@ -1,22 +1,46 @@ # frozen_string_literal: true +require 'logger' + module Facter RED = 31 class Log @@file_logger = Logger.new(File.new("#{ROOT_DIR}/example.log", 'a')) - @@legacy_logger = LegacyLogger.new - @@logger = MultiLogger.new([@@legacy_logger, @@file_logger]) + @@legacy_logger = nil + @@logger = MultiLogger.new([@@file_logger]) @@logger.level = :warn + @@message_callback = nil + class << self + def on_message(&block) + @@message_callback = block + end + + def level=(log_level) + @@logger.level = log_level + end + + def level + @@logger.level + end + end + def initialize(logged_class) determine_callers_name(logged_class) set_format_for_file_logger - set_format_for_stdout end + def self.add_legacy_logger(output) + return if @@legacy_logger + + @@legacy_logger = Logger.new(output) + set_format_for_legacy_logger + @@logger.add_logger(@@legacy_logger) + end + def determine_callers_name(sender_self) @class_name = sender_self.class.name != 'Class' ? sender_self.class.name : sender_self.name end def set_format_for_file_logger @@ -24,26 +48,27 @@ datetime = datetime.strftime(@datetime_format || '%Y-%m-%d %H:%M:%S.%6N ') "[#{datetime}] #{severity} #{msg} \n" end end - def set_format_for_stdout + def self.set_format_for_legacy_logger @@legacy_logger.formatter = proc do |severity, datetime, _progname, msg| datetime = datetime.strftime(@datetime_format || '%Y-%m-%d %H:%M:%S.%6N ') "[#{datetime}] #{severity} #{msg} \n" end end - def self.level=(log_level) - @@logger.level = log_level - end - - def self.level - @@logger.level - end - def debug(msg) - @@logger.debug(@class_name + ' - ' + msg) + return unless Facter.debugging? + + if msg.nil? || msg.empty? + invoker = caller(1..1).first.slice(/.*:\d+/) + self.warn "#{self.class}#debug invoked with invalid message #{msg.inspect}:#{msg.class} at #{invoker}" + elsif @@message_callback + @@message_callback.call(:debug, msg) + else + @@logger.debug(@class_name + ' - ' + msg) + end end def info(msg) @@logger.info(@class_name + ' - ' + msg) end