Andy Weiss
Andy Weiss

Reputation: 435

How do I set the PaperTrail logger?

I am running into an issue when trying to reify PaperTrail versions after removing a column from the model's table.

The stacktrace begins: private method 'warn' called for nil:NilClass

and points me to the following lines inside of the reify method:

# Set all the attributes in this version on the model.
attrs.each do |k, v|
  if model.has_attribute?(k)
    model[k.to_sym] = v
  elsif model.respond_to?("#{k}=")
    model.send("#{k}=", v)
  else
    logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
  end
end

Because I have removed the column from the table, I am landing in the else block of the logic tree, which seems perfectly reasonable... I am fine logging this out moving on with the reification.

However, I don't understand why logger is nil in the first place. Where and how can I set the PaperTrail logger so that we simply log the behavior instead of crashing the app?

Upvotes: 1

Views: 159

Answers (2)

Jared Beck
Jared Beck

Reputation: 17528

As of 2016-12-14 the answer is to upgrade to PT 6.0.2.

This was fixed in PT 6.0.2, thanks to PR https://github.com/airblade/paper_trail/pull/905 by Andy (the OP. Thanks, Andy)

Upvotes: 1

Andy Weiss
Andy Weiss

Reputation: 435

I figured out two ways to accomplish this.

1) By overwriting the PaperTrail::Version class

module PaperTrail
  class Version < ActiveRecord::Base
    include PaperTrail::VersionConcern

    def logger
      Logger.new(STDOUT)
    end

  end
end

2) By setting ActiveRecord::Base's logger in an initializer (obviously this affects all models, so you may want to specify the log level to something higher than debug):

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::INFO

Curious if there is another recommended way, but these appear to work for my purposes for now.

Upvotes: 0

Related Questions