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

TaggedLogging to return a new logger instance #27792

Merged
merged 1 commit into from Sep 11, 2018

Conversation

Projects
None yet
7 participants
@tjoyal
Contributor

tjoyal commented Jan 24, 2017

Summary

I have a small question and what is better than a PR to discuss it.

ActiveSupport::TaggedLogging through the new method would mutate the argument instead of returning a new instance. This might be my design but it really got me confused at first. I would have expected some other kind of method to provide such mutation (eg.: ActiveSupport::TaggedLogging.tag(logger))

The changes in here would clone the logger received in argument (and the associated formatter that ActiveSupport::TaggedLogging perform on) returning a new instance.

The associated test is what made me really confused the first time I hit the original behaviour.

Todo:

  • If this go forward it'll require an entry in changelog?
@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot Jan 24, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @arthurnn (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

rails-bot commented Jan 24, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @arthurnn (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@arthurnn

This comment has been minimized.

Show comment
Hide comment
@arthurnn

arthurnn Jan 31, 2017

Member

I agree that new should return a new object, and not mutate the existent one.
@rafaelfranca @matthewd thoughts?
I am 👍 here

Member

arthurnn commented Jan 31, 2017

I agree that new should return a new object, and not mutate the existent one.
@rafaelfranca @matthewd thoughts?
I am 👍 here

@rafaelfranca

LGTM too. A CHANGELOG entry will be required since this may be a breaking change.

Show outdated Hide outdated activesupport/lib/active_support/tagged_logging.rb

@tjoyal tjoyal changed the title from Sandbox tagged logging to TaggedLogging to return a new logger instance Feb 1, 2017

@tjoyal

This comment has been minimized.

Show comment
Hide comment
@tjoyal

tjoyal Feb 1, 2017

Contributor

Updated, tell me if you are good with the changes.

Contributor

tjoyal commented Feb 1, 2017

Updated, tell me if you are good with the changes.

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd Feb 1, 2017

Member

Does this risk multiplying the number of live logger instances we have in an application?

That seems like it could affect what gets silenced, for example.

Member

matthewd commented Feb 1, 2017

Does this risk multiplying the number of live logger instances we have in an application?

That seems like it could affect what gets silenced, for example.

@tjoyal

This comment has been minimized.

Show comment
Hide comment
@tjoyal

tjoyal Feb 2, 2017

Contributor

Does this risk multiplying the number of live logger instances we have in an application?

My thought of chain on this is when calling new, I expect to get a new object.

I think it's a fair concern, and to some extent I would consider setting my Rails.logger to return a tagged logger from the ground up if I was planning to tag a lot of things. (vs re-init everywhere in the code leading to a lot of code duplication).

I think it would be possible to come up with a mutating method that is not named new that would have the old behaviour, but I don't see the need for it just yet.

That seems like it could affect what gets silenced, for example.

Definitely (it's a new instance after all, unless dup decide to handle logic to bridge behaviour between instances).

But once again, I think this is expected from a method that is called new.

As proposed up there, if one was to use a lot of tagged logging in his application, I think it would be fair to consider setting it as the default logger.

In the cases where I wanted to use tagged logging, the desired behaviour was to do a dup like in this PR so my tags are only applied to the current context of the unit of code I'm wrapping, and not propagated to other unit of code that might be called in other classes.

Eg.: A fairly simple example

class Object
  def method_a
    logger = ActiveSupport::TaggedLogging.new(Rails.logger)
    logger.tagged("BCX") do
      logger.info "something" # I expected this to be tagged
      method_b
    end
  end

  def method_b
    Rails.logger.info "something else" # I do not expect this to be tagged
  end
end
Contributor

tjoyal commented Feb 2, 2017

Does this risk multiplying the number of live logger instances we have in an application?

My thought of chain on this is when calling new, I expect to get a new object.

I think it's a fair concern, and to some extent I would consider setting my Rails.logger to return a tagged logger from the ground up if I was planning to tag a lot of things. (vs re-init everywhere in the code leading to a lot of code duplication).

I think it would be possible to come up with a mutating method that is not named new that would have the old behaviour, but I don't see the need for it just yet.

That seems like it could affect what gets silenced, for example.

Definitely (it's a new instance after all, unless dup decide to handle logic to bridge behaviour between instances).

But once again, I think this is expected from a method that is called new.

As proposed up there, if one was to use a lot of tagged logging in his application, I think it would be fair to consider setting it as the default logger.

In the cases where I wanted to use tagged logging, the desired behaviour was to do a dup like in this PR so my tags are only applied to the current context of the unit of code I'm wrapping, and not propagated to other unit of code that might be called in other classes.

Eg.: A fairly simple example

class Object
  def method_a
    logger = ActiveSupport::TaggedLogging.new(Rails.logger)
    logger.tagged("BCX") do
      logger.info "something" # I expected this to be tagged
      method_b
    end
  end

  def method_b
    Rails.logger.info "something else" # I do not expect this to be tagged
  end
end
@gmcgibbon

Still relevant in Rails 6 28e5085. I'll admit that when I first saw the source for ActiveSupport::TaggedLogging.new I thought it was being too clever. If we want to keep the mutation approach, could we just rename the new method to something more explanatory?

@rafaelfranca rafaelfranca merged commit 62fba74 into rails:master Sep 11, 2018

2 checks passed

codeclimate no new or fixed issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

rafaelfranca added a commit that referenced this pull request Sep 11, 2018

Merge pull request #27792 from tjoyal/sandbox-tagged-logging
TaggedLogging to return a new logger instance

@tjoyal tjoyal deleted the tjoyal:sandbox-tagged-logging branch Sep 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment