Module: Legion::Logging::Helper

Constant Summary collapse

CompatLogger =
Class.new do
  %i[debug info warn error fatal unknown].each do |level|
    define_method(level) do |message = nil, &block|
      payload = block ? block.call : message
      return if payload.nil?

      if logging_supports?(level)
        Legion::Logging.public_send(level, payload)
      elsif %i[error fatal warn].include?(level)
        ::Kernel.warn(payload)
      else
        $stdout.puts(payload)
      end
    end
  end

  private

  def logging_supports?(level)
    return false unless Legion.const_defined?('Logging')

    Legion::Logging.respond_to?(level)
  rescue StandardError
    false
  end
end

Instance Method Summary collapse

Instance Method Details

#handle_exception(exception, task_id: nil, level: :error, handled: true, **opts) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument,Style/ArgumentsForwarding



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/legion/logging/helper.rb', line 51

def handle_exception(exception, task_id: nil, level: :error, handled: true, **opts) # rubocop:disable Lint/UnusedMethodArgument,Style/ArgumentsForwarding
  message = exception_log_message(exception, level: level, **opts) # rubocop:disable Style/ArgumentsForwarding

  if logging_supports?(:log_exception)
    Legion::Logging.log_exception(exception, level: level, lex: 'crypt', component_type: :helper)
    return
  end
  if logging_supports?(level)
    Legion::Logging.public_send(level, message)
    return
  end
  if logging_supports?(:error)
    Legion::Logging.error(message)
    return
  end
  if logging_supports?(:warn)
    Legion::Logging.warn(message)
    return
  end

  ::Kernel.warn(message)
end

#logObject



47
48
49
# File 'lib/legion/logging/helper.rb', line 47

def log
  @log ||= CompatLogger.new
end