Skip to content

Fix TaggedLogging to allow loggers to be instantiated multiple times without having to share the stack of tags#9065

Merged
rafaelfranca merged 1 commit into
rails:masterfrom
atombender:master
Jan 2, 2015
Merged

Fix TaggedLogging to allow loggers to be instantiated multiple times without having to share the stack of tags#9065
rafaelfranca merged 1 commit into
rails:masterfrom
atombender:master

Conversation

@atombender
Copy link
Copy Markdown

This is accomplished by using a unique key for the thread-local tag list. Fixes #9064.

…without having to share the stack of tags. This is accomplished by using a unique key for the thread-local tag list. Fixes rails#9064.
@jeremy
Copy link
Copy Markdown
Member

jeremy commented Jan 24, 2013

Looks good. Having a thread-local formatter might feel nicer, but it's a hassle to implement. /cc @tenderlove

@carlosantoniodasilva
Copy link
Copy Markdown
Member

Don't we have Thread local configs now, that could be used for it?

@atombender
Copy link
Copy Markdown
Author

@carlosantoniodasilva: The tagged logger already uses a thread-local. The problem is that it acts as if there can only be a single tagged logger instance.

Here's my example from the previous issue (sorry about the dupe, didn't know Github listed pull requests in the issue tracker):

my_logger = ActiveSupport::TaggedLogging.new(Rails.logger)
my_logger.push_tags "my_tag"
my_logger.info "test"  # outputs "[my_tag] [my_tag] test"
Rails.logger.info "test"  # outputs "[my_tag] test"

It's also not safe to wrap a unique logger (ie., one that does not delegate to a logger shared around in the app, like Rails.logger):

my_logger = ActiveSupport::TaggedLogging.new(Logger.new($stdout))
my_logger.push_tags "my_tag"
Rails.logger.info "test"  # outputs "[my_tag] test"
Rails.logger.tagged("other_tag") do
  my_logger.info "test"  # outputs "[my_tag] [other_tag]"
end

In other words, TaggedLogging is effectively a singleton within a single thread.

The fix uses a unique key to store the thread-local tag list. (The only weakness is that the thread-local key will never be garbage collected until the thread dies, but it seems unrealistic that anyone would create a huge number of loggers.)

@ches
Copy link
Copy Markdown

ches commented Sep 12, 2013

👍

I wanted this behavior and expected it to just work. Particularly because current_tags is undocumented and was private in 3.2, one had to go to the source to see why it doesn't. I smacked my head when I got there.

rafaelfranca added a commit that referenced this pull request Jan 2, 2015
Fix TaggedLogging to allow loggers to be instantiated multiple times without having to share the stack of tags
@rafaelfranca rafaelfranca merged commit eed68fd into rails:master Jan 2, 2015
@stephankaag
Copy link
Copy Markdown

@rafaelfranca Was this change ever released? Looks like it's not included in ActiveSupport 4.2.

https://github.com/rails/rails/blob/4-2-stable/activesupport/lib/active_support/tagged_logging.rb#L46

@atombender
Copy link
Copy Markdown
Author

@stephankaag: No, for some reason it's still only on master, even though it was merged more than 10 months ago. Not sure how the release process works, but I'm assuming it will end up in 4.3 (or 5.0).

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jun 13, 2016
rafaelfranca added a commit that referenced this pull request Jun 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ActiveSupport::TaggedLogging shares tags between instances, cannot be instantiated individually

6 participants