Skip to content

Commit 78d91c9

Browse files
committed
fix missing call method for formatter proxy
1 parent e275b5f commit 78d91c9

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

lib/log_struct/semantic_logger/logger.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ def <<(msg)
148148
end
149149

150150
# Proxy object to provide ActiveJob-compatible formatter interface
151-
class FormatterProxy
151+
# Also implements the standard Logger formatter interface (call method)
152+
# for compatibility with Ruby's Logger (especially logger gem 1.7.0+)
153+
class FormatterProxy < ::Logger::Formatter
152154
extend T::Sig
153155

154156
sig { returns(T::Array[T.any(String, Symbol)]) }

test/log_struct/semantic_logger_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,56 @@ class SemanticLoggerTest < ActiveSupport::TestCase
220220
::SemanticLogger.flush # Flush to ensure output is written
221221
end
222222
end
223+
224+
test "FormatterProxy responds to call method for Logger compatibility" do
225+
formatter = LogStruct::SemanticLogger::FormatterProxy.new
226+
227+
# FormatterProxy must implement the standard Logger formatter interface
228+
assert_respond_to formatter, :call
229+
assert_respond_to formatter, :current_tags
230+
231+
# call method should accept standard Logger formatter arguments
232+
time = Time.now
233+
result = formatter.call("INFO", time, "TestProgram", "Test message")
234+
235+
# Should return a string
236+
assert_kind_of String, result
237+
assert_includes result, "Test message"
238+
end
239+
240+
test "FormatterProxy works with standard Ruby Logger" do
241+
# This test ensures FormatterProxy can be used as a formatter for Ruby's Logger
242+
# which is important for Rails/ActiveSupport compatibility (logger 1.7.0+)
243+
io = StringIO.new
244+
stdlib_logger = ::Logger.new(io)
245+
246+
# Assign FormatterProxy as the formatter (simulates what Rails might do)
247+
stdlib_logger.formatter = LogStruct::SemanticLogger::FormatterProxy.new
248+
249+
# Should not raise an error when logging
250+
assert_nothing_raised do
251+
stdlib_logger.info("Test message from stdlib logger")
252+
end
253+
254+
# Output should contain the message
255+
output = io.string
256+
257+
assert_includes output, "Test message from stdlib logger"
258+
end
259+
260+
test "FormatterProxy call method handles nil and empty messages" do
261+
formatter = LogStruct::SemanticLogger::FormatterProxy.new
262+
time = Time.now
263+
264+
# Should handle nil message
265+
result = formatter.call("INFO", time, nil, nil)
266+
267+
assert_kind_of String, result
268+
269+
# Should handle empty string message
270+
result = formatter.call("DEBUG", time, "prog", "")
271+
272+
assert_kind_of String, result
273+
end
223274
end
224275
end

0 commit comments

Comments
 (0)