class Dunlop::LogEntry < ApplicationRecord
  belongs_to :loggable, polymorphic: true, optional: true

  MAX_BODY_SIZE = (2**16-1)
  before_save :truncate_body

  def truncate_body
    self.body = body.to_s.truncate(MAX_BODY_SIZE)
  end

  class << self
    #TODO: remove me if it is confirmed that it is not used in applications
    def exception_to_body(e)
      [
        e.message,
        Array.wrap(e.backtrace)[0..9],
      ].flatten.join("\n")
    end

    def exception_to_backtrace(e)
      Array.wrap(e.backtrace).first(30).map { |line| line.to_s.sub(/#{Gem.dir}\/gems\/|#{Rails.root}\//, '') }.join("\n")
    end
  end
end

