Skip to content

Commit fa01d04

Browse files
HolyWalleyioquatix
authored andcommitted
Build and pass tags as options to logger.
1 parent 173fa21 commit fa01d04

6 files changed

Lines changed: 77 additions & 3 deletions

File tree

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Yasha Krasnou <yakau.krasnou@44pixels.ai>

console-adapter-rails.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
77
spec.version = Console::Adapter::Rails::VERSION
88

99
spec.summary = "Adapt Rails logs and events to the console gem."
10-
spec.authors = ["Samuel Williams", "Joshua Young", "Jun Jiang", "Michael Adams"]
10+
spec.authors = ["Samuel Williams", "Joshua Young", "Jun Jiang", "Michael Adams", "Yasha Krasnou"]
1111
spec.license = "MIT"
1212

1313
spec.cert_chain = ["release.cert"]
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
2424

2525
spec.required_ruby_version = ">= 3.3"
2626

27-
spec.add_dependency "console", "~> 1.21"
27+
spec.add_dependency "console", "~> 1.34"
2828
spec.add_dependency "fiber-storage", "~> 1.0"
2929
spec.add_dependency "rails", ">= 7.0"
3030
end

lib/console/adapter/rails/logger.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the MIT License.
44
# Copyright, 2023-2025, by Samuel Williams.
55
# Copyright, 2024, by Michael Adams.
6+
# Copyright, 2026, by Yasha Krasnou.
67

78
require "console/compatible/logger"
89

@@ -83,7 +84,25 @@ def silenced?(severity)
8384
def add(severity, message = nil, progname = nil, &block)
8485
return if silenced?(severity)
8586

86-
super(severity, message, progname, &block)
87+
if formatter.respond_to?(:tag_stack)
88+
tags = formatter.tag_stack.tags
89+
90+
options = tags.each_with_object({}) do |tag, memo|
91+
next unless tag.respond_to?(:to_hash)
92+
93+
tag.to_hash.each do |key, value|
94+
case key
95+
when Symbol
96+
memo[key] = value
97+
else
98+
next unless key.respond_to?(:to_sym)
99+
memo[key.to_sym] = value
100+
end
101+
end
102+
end
103+
end
104+
105+
super(severity, message, progname, **options, &block)
87106
end
88107

89108
# Configure Rails to use the Console logger.

license.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Copyright, 2023-2026, by Samuel Williams.
44
Copyright, 2023, by Joshua Young.
55
Copyright, 2024, by Michael Adams.
66
Copyright, 2025, by Jun Jiang.
7+
Copyright, 2026, by Yasha Krasnou.
78

89
Permission is hereby granted, free of charge, to any person obtaining a copy
910
of this software and associated documentation files (the "Software"), to deal

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Add support for Rails tagged logging in the Rails logger adapter. This allows log messages with tags to be properly formatted and displayed.
6+
37
## v0.5.0
48

59
- Improved compatibilty with Rails 8+.

test/console/adapter/rails/logger.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the MIT License.
44
# Copyright, 2023-2026, by Samuel Williams.
55
# Copyright, 2024, by Michael Adams.
6+
# Copyright, 2026, by Yasha Krasnou.
67

78
require "app"
89
require "console/capture"
@@ -21,6 +22,54 @@ def before
2122
expect(Rails.logger).to be(:respond_to?, :tagged)
2223
end
2324

25+
it "should log tags that are Hashes" do
26+
Rails.logger.tagged({ foo: "bar" }) do
27+
Rails.logger.info("Hello World")
28+
end
29+
30+
expect(capture.last).to have_keys(
31+
message: be == "Hello World",
32+
foo: be == "bar"
33+
)
34+
end
35+
36+
it "should not fail when logging string tags" do
37+
Rails.logger.tagged("foo=bar") do
38+
Rails.logger.info("Hello World")
39+
end
40+
41+
expect(capture.last).to have_keys(
42+
message: be == "Hello World"
43+
)
44+
45+
expect(capture.last).not.to have_keys(:foo)
46+
expect(capture.last).not.to have_keys("foo=bar")
47+
end
48+
49+
it "should symbolize string-keyed hash tags" do
50+
Rails.logger.tagged({ "request_id" => "abc" }) do
51+
Rails.logger.info("Hello World")
52+
end
53+
54+
expect(capture.last).to have_keys(
55+
message: be == "Hello World",
56+
request_id: be == "abc"
57+
)
58+
expect(capture.last).not.to have_keys("request_id")
59+
end
60+
61+
it "should symbolize HashWithIndifferentAccess tags" do
62+
Rails.logger.tagged(ActiveSupport::HashWithIndifferentAccess.new(request_id: "xyz")) do
63+
Rails.logger.info("Hello World")
64+
end
65+
66+
expect(capture.last).to have_keys(
67+
message: be == "Hello World",
68+
request_id: be == "xyz"
69+
)
70+
expect(capture.last).not.to have_keys("request_id")
71+
end
72+
2473
it "should support silence" do
2574
expect(Rails.logger).to be(:respond_to?, :silence)
2675

0 commit comments

Comments
 (0)