Skip to content

Commit 592a52b

Browse files
authored
Add TaggedLogging#logger constructor for more pleasant logging interface (rails#52994)
1 parent 640d0f6 commit 592a52b

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

activesupport/lib/active_support/tagged_logging.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ def self.extended(base)
113113
end
114114
end
115115

116+
# Returns an `ActiveSupport::Logger` that has already been wrapped with tagged logging concern.
117+
def self.logger(*args, **kwargs)
118+
new ActiveSupport::Logger.new(*args, **kwargs)
119+
end
120+
116121
def self.new(logger)
117122
logger = logger.clone
118123

activesupport/test/tagged_logging_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ def flush(*)
142142

143143
assert_equal "[BCX] [Jason] Funky time\n[BCX] Junky time!\n", @output.string
144144
end
145+
146+
test "implicit logger instance" do
147+
@output = StringIO.new
148+
@logger = ActiveSupport::TaggedLogging.logger(@output)
149+
150+
@logger.tagged("BCX") { @logger.info "Funky time" }
151+
assert_equal "[BCX] Funky time\n", @output.string
152+
end
145153
end
146154

147155
class TaggedLoggingWithoutBlockTest < ActiveSupport::TestCase

railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Rails.application.configure do
5555

5656
# Log to STDOUT with the current request id as a default log tag.
5757
config.log_tags = [ :request_id ]
58-
config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
58+
config.logger = ActiveSupport::TaggedLogging.logger(STDOUT)
5959

6060
# "info" includes generic and useful information about system operation, but avoids logging too much
6161
# information to avoid inadvertent exposure of personally identifiable information (PII). If you

0 commit comments

Comments
 (0)