diff --git a/lib/console/capture.rb b/lib/console/capture.rb index fd3e354..a92dfdf 100644 --- a/lib/console/capture.rb +++ b/lib/console/capture.rb @@ -55,6 +55,10 @@ def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block) message[:arguments] = arguments end + if annotation = Fiber.current.annotation + message[:annotation] = annotation + end + if block_given? if block.arity.zero? message[:message] = yield diff --git a/lib/console/terminal/logger.rb b/lib/console/terminal/logger.rb index ffcac11..2564b19 100644 --- a/lib/console/terminal/logger.rb +++ b/lib/console/terminal/logger.rb @@ -149,8 +149,14 @@ def default_suffix(object = nil) buffer = +"" if @verbose - if annotation = Fiber.current.annotation and annotation.size > 0 - buffer << ": #{@terminal[:annotation]}#{annotation}#{@terminal.reset}" + if annotation = Fiber.current.annotation + # While typically annotations should be strings, that is not always the case. + annotation = annotation.to_s + + # If the annotation is empty, we don't want to print it, as it will look like a formatting bug. + if annotation.size > 0 + buffer << ": #{@terminal[:annotation]}#{annotation}#{@terminal.reset}" + end end end diff --git a/test/console/logger.rb b/test/console/logger.rb index c868610..1b2b27f 100644 --- a/test/console/logger.rb +++ b/test/console/logger.rb @@ -154,4 +154,19 @@ def after expect(Console::Logger.default_log_level({'CONSOLE_LEVEL' => 'debug'})).to be == Console::Logger::DEBUG end end + + with "Fiber annotation" do + it "logs fiber annotations" do + Fiber.new do + Fiber.annotate("Running in a fiber.") + + logger.info(message) + end.resume + + expect(output.last).to have_keys( + annotation: be == "Running in a fiber.", + subject: be == "Hello World", + ) + end + end end diff --git a/test/console/serialized/logger.rb b/test/console/serialized/logger.rb index 60d61cd..fdbbe40 100644 --- a/test/console/serialized/logger.rb +++ b/test/console/serialized/logger.rb @@ -59,4 +59,34 @@ expect(error_message).to have_keys(:kind, :message, :stack) end end + + with "Fiber annotation" do + it "logs fiber annotations" do + Fiber.new do + Fiber.annotate("Running in a fiber.") + + logger.call(message) + end.resume + + expect(record).to have_keys( + annotation: be == "Running in a fiber.", + subject: be == "Hello World", + ) + end + + it "logs fiber annotations when it isn't a string" do + thing = ["Running in a fiber."] + + Fiber.new do + Fiber.annotate(thing) + + logger.call(message) + end.resume + + expect(record).to have_keys( + annotation: be == thing, + subject: be == "Hello World", + ) + end + end end diff --git a/test/console/terminal/logger.rb b/test/console/terminal/logger.rb index 5827266..cb36da5 100644 --- a/test/console/terminal/logger.rb +++ b/test/console/terminal/logger.rb @@ -35,4 +35,28 @@ expect(io.string).to be(:include?, message) end end + + with "Fiber annotation" do + it "logs fiber annotations" do + Fiber.new do + Fiber.annotate("Running in a fiber.") + + logger.call(message) + end.resume + + expect(io.string).to be(:include?, "Running in a fiber.") + end + + it "logs fiber annotations when it isn't a string" do + thing = ["Running in a fiber."] + + Fiber.new do + Fiber.annotate(thing) + + logger.call(message) + end.resume + + expect(io.string).to be(:include?, thing.to_s) + end + end end