<commit_before>require "logger"

module BmcDaemonLib
  module LoggerHelper
    # Use accessor to expose logger to Grape, who uses .logger
    attr_accessor :logger

  protected

    def log_pipe pipe, caller = nil
      @log_pipe = pipe
      @logger = BmcDaemonLib::LoggerPool.instance.get pipe
      @caller = caller
      #Conf.log "log #{@log_pipe}", "log_to pipe: #{pipe} logger class: #{@logger.class}"
      #return @logger
    end

    def log_context
      {}      # ['DEFAULT', self.class.name.split('::').last]
    end

    def log_info message, details = nil
      logger.add Logger::INFO, message, get_full_context, details
    end
    def log_error message, details = nil
      logger.add Logger::ERROR, message, get_full_context, details
    end
    def log_debug message, details = nil
      logger.add Logger::DEBUG, message, get_full_context, details
    end

  private

    def get_full_context
      context = nil

      # Grab the classe's context
      context = log_context() if self.respond_to?(:log_context)

      # Initialize an empty context, if log_context returned something else, or it the method was not exposed
      context = {} unless context.is_a? Hash

      # Who is the caller? Guess it from caller's class name if not provided
      context[:caller] ||= self.class.name.to_s.split('::').last

      # Return the whole context
      return context
    end

    # alias info log_info
    # alias error log_error
    # alias debug log_debug

  end
end<commit_msg>logger helper: add check on @logger presence, extract get_class_name<commit_after>require "logger"

module BmcDaemonLib
  module LoggerHelper
    # Use accessor to expose logger to Grape, who uses .logger
    attr_accessor :logger

  protected

    def log_pipe pipe, caller = nil
      @log_pipe = pipe
      @logger = BmcDaemonLib::LoggerPool.instance.get pipe
      @caller = caller
      #Conf.log "log #{@log_pipe}", "log_to pipe: #{pipe} logger class: #{@logger.class}"
      #return @logger
    end

    def log_context
      {}      # ['DEFAULT', self.class.name.split('::').last]
    end

    def log_info message, details = nil
      log Logger::INFO, message, details
    end
    def log_error message, details = nil
      log Logger::ERROR, message, details
    end
    def log_debug message, details = nil
      log Logger::DEBUG, message, details
    end

  private

    def get_full_context
      context = nil
    def log severity, message, details
      return puts "LoggerHelper.log: missing logger (#{get_class_name})" unless logger
      # puts "LoggerHelper.log > #{message}"
      # puts "LoggerHelper.log     > #{get_full_context.inspect}"
      logger.add severity, message, get_full_context, details
    end

      # Grab the classe's context
      context = log_context() if self.respond_to?(:log_context)

      # Initialize an empty context, if log_context returned something else, or it the method was not exposed
      context = {} unless context.is_a? Hash

      # Who is the caller? Guess it from caller's class name if not provided
      context[:caller] ||= get_class_name

      # Return the whole context
      return context
    end

    # alias info log_info
    # alias error log_error
    # alias debug log_debug
    def get_class_name
      self.class.name.to_s.split('::').last
    end

  end
end
{'fraction_non_alphanumeric': 0.0859538784067086, 'fraction_numerical': 0.0, 'mean_word_length': 2.8434982738780206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1539779', 'n_tokens_mistral': 985, 'n_tokens_neox': 954, 'n_words': 327}