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

Convert annotation to string before printing. #53

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/console/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/console/terminal/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions test/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions test/console/serialized/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions test/console/terminal/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading