def log_internal(level, index, message = nil, payload = nil, exception = nil)
      # Exception being logged?
      if exception.nil? && payload.nil? && message.respond_to?(:backtrace) && message.respond_to?(:message)
        exception = message
        message   = nil
      elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message)
        exception = payload
        payload   = nil
      end

      # Add result of block as message or payload if not nil
      if block_given? && (result = yield)
        if result.is_a?(String)
          message = message.nil? ? result : "#{message} -- #{result}"
        elsif message.nil? && result.is_a?(Hash)
          message = result
        elsif payload && payload.respond_to?(:merge) # rubocop: disable Style/SafeNavigation
          payload.merge(result)
        else
          payload = result
        end
      end

      # Add scoped payload
      if self.payload
        payload = payload.nil? ? self.payload : self.payload.merge(payload)
      end

      merged_tags = merge_tags_with_payload(payload)
      # Add caller stack trace
      backtrace = extract_backtrace if index >= Sapience.config.backtrace_level_index

      log = Log.new(level, Thread.current.name, name, message, payload, Time.now, nil, merged_tags, index, exception, nil, backtrace)

      # Logging Hash only?
      # logger.info(name: 'value')

      if payload.nil? && exception.nil? && message.is_a?(Hash)
        payload           = message.dup
        min_duration      = payload.delete(:min_duration) || 0.0
        log.exception     = payload.delete(:exception)
        log.message       = payload.delete(:message)
        log.metric        = payload.delete(:metric)
        log.metric_amount = payload.delete(:metric_amount) || 1
        if (duration = payload.delete(:duration))
          return false if duration <= min_duration
          log.duration = duration
        end
        log.payload = payload unless payload.empty?
      end
      self.log(log) if include_message?(log)
    end
def log_internal(level, index, message = nil, payload = nil, exception = nil)
      # Exception being logged?
      if exception.nil? && payload.nil? && message.respond_to?(:backtrace) && message.respond_to?(:message)
        exception = message
        message   = nil
      elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message)
        exception = payload
        payload   = nil
      end

      # Add result of block as message or payload if not nil
      if block_given? && (result = yield)
        if result.is_a?(String)
          message = message.nil? ? result : "#{message} -- #{result}"
        elsif message.nil? && result.is_a?(Hash)
          message = result
        elsif payload && payload.respond_to?(:merge) # rubocop: disable Style/SafeNavigation
          payload.merge(result)
        else
          payload = result
        end
      end

      # Add scoped payload
      if self.payload
        payload = payload.nil? ? self.payload : self.payload.merge(payload)
      end

      merged_tags = merge_tags_with_payload(payload)
      # Add caller stack trace
      backtrace = extract_backtrace if index >= Sapience.config.backtrace_level_index

      log = Log.new(level, Thread.current.name, name, message, payload, Time.now, nil, merged_tags, index, exception, nil, backtrace)

      # Logging Hash only?
      # logger.info(name: 'value')

      if payload.nil? && exception.nil? && message.is_a?(Hash)
        payload           = message.dup
        min_duration      = payload.delete(:min_duration) || 0.0
        log.exception     = payload.delete(:exception)
        log.message       = payload.delete(:message)
        log.metric        = payload.delete(:metric)
        log.metric_amount = payload.delete(:metric_amount) || 1
        if (duration = payload.delete(:duration))
          return false if duration <= min_duration
          log.duration = duration
        end
        log.payload = payload unless payload.empty?
      end
      self.log(log) if include_message?(log)
    end
TODO: Move this to logger.rb?
 Log message at the specified level
 rubocop:disable AbcSize, PerceivedComplexity, CyclomaticComplexity, LineLength