Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only warn logger if present #905

Merged

Conversation

andyweiss1982
Copy link
Contributor

@andyweiss1982 andyweiss1982 commented Dec 5, 2016

Currently it is possible to run into this error message: private method 'warn' called for nil:NilClass, which will crash apps that do not have ActiveRecord::Base.logger set.

It's a little confusing to draw a straight line from the original source of the error ("Attribute #{k} does not exist on #{version.item_type} (Version id: #{version.id}).") to the error message currently displayed in the console.

I would think it would be preferable to only call warn on a logger if one is present, eliminating possible sources of 500 errors and allowing developers to use paper_trail without explicitly setting a logger class for PaperTrail::Version or ActiveRecord::Base

@@ -502,7 +502,7 @@ def habtm_assoc_ids(habtm_assoc)
end

def log_version_errors(version, action)
version.logger.warn(
version.logger&.warn(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the ampersand character do in this context?

Does it not make more sense to just wrap these statements on a if block that checks to see if version.logger is not nil?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think I understand now, however according to this stackoverflow post, this functionality was introduced in Ruby 2.3, which would make the gem unusable with earlier Ruby versions, which is not desirable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK that is certainly a fair critique. I can resubmit with the conditional inlined explicitly.

Copy link
Collaborator

@batter batter Dec 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great. I think we can just wrap it in a if version.logger...

The question is whether there should be an else which logs it to STDOUT or something, do you have thoughts on that?

@andyweiss1982
Copy link
Contributor Author

@batter what do you make of the most recent change, replacing version.logger with version.logger || Logger.new(STDOUT)?

@jaredbeck
Copy link
Member

@batter what do you make of the most recent change, replacing version.logger with version.logger || Logger.new(STDOUT)?

This will prevent people from disabling output, which I assume is the goal of setting ActiveRecord::Base.logger to nil. So, I don't think it's a good idea.

@batter
Copy link
Collaborator

batter commented Dec 5, 2016

I suppose @jaredbeck is probably right. Personally I would prefer to see feedback but I know not everyone wants that, and we've gotten complaints in the past from some users who had difficulty silencing warnings that PaperTrail was passing, so I think the original proposed if statement wrap is probably the best approach.

@andyweiss1982
Copy link
Contributor Author

@batter sounds good, changed it back to plain vanilla if version.logger

@batter
Copy link
Collaborator

batter commented Dec 5, 2016

Looks good to me, and the tests are passing. Can you squash this into a single commit, then I think it's ready to merge, cheers!

@andyweiss1982
Copy link
Contributor Author

Great, all squashed. Thanks!

@batter batter merged commit 5d37867 into paper-trail-gem:master Dec 5, 2016
@batter
Copy link
Collaborator

batter commented Dec 5, 2016

Cheers!

batter added a commit that referenced this pull request Dec 5, 2016
@jaredbeck
Copy link
Member

Released in 6.0.2

aried3r pushed a commit to aried3r/paper_trail that referenced this pull request Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants