Sha256: e14762d2246c44ed1ae9345f8f885a0fed91d9a1cb59ef562da9e171923b4265

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true
require 'time'
require 'securerandom'

class RailsLogParser::Action
  class << self
    attr_accessor :last
  end

  SEVERITIES = %i[debug info warn error fatal].freeze
  KNOWN_EXCEPTIONS = {
    'ActiveRecord::RecordNotFound' => :fatal,
    'ActionController::RoutingError' => :fatal,
    "Can't verify CSRF token authenticity." => :warn,
    'ActionController::InvalidAuthenticityToken' => :fatal,
  }.freeze

  extend Enumerize
  enumerize :severity, in: SEVERITIES, predicates: true
  enumerize :type, in: %i[request without_request delayed_job active_job], predicates: true

  def initialize(type, id)
    self.type = type
    @id = id
    @messages = []
    @stacktrace = []
    self.class.last = self
  end

  def severity=(value)
    value = value.downcase.to_sym
    return unless severity.nil? || SEVERITIES.index(severity.to_sym) < SEVERITIES.index(value)

    super(value)
    @headline = nil
  end

  def known_exception?
    @messages.any? do |message|
      KNOWN_EXCEPTIONS.any? { |e, s| message.include?(e) && severity == s }
    end
  end

  def headline
    @headline.presence || @messages.first
  end

  def datetime=(value)
    @datetime ||= Time.parse(value)
  end

  def after?(datetime)
    @datetime > datetime
  end

  def add_message(value)
    @messages.push(value)
    @headline = value if @headline.nil?
  end

  def add_stacktrace(value)
    @stacktrace.push(value)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_log_parser-0.0.5 lib/rails_log_parser/action.rb