diff --git a/lib/appsignal/logger.rb b/lib/appsignal/logger.rb index 6cc9a3c5..5644e261 100644 --- a/lib/appsignal/logger.rb +++ b/lib/appsignal/logger.rb @@ -38,6 +38,7 @@ def initialize(group, level: INFO, format: PLAINTEXT, attributes: {}) @mutex = Mutex.new @default_attributes = attributes @appsignal_attributes = {} + @loggers = [] end # We support the various methods in the Ruby @@ -60,6 +61,12 @@ def add(severity, message = nil, group = nil) message = formatter.call(severity, Time.now, group, message) if formatter + @loggers.each do |logger| + logger.add(severity, message, group) + rescue + nil + end + unless message.is_a?(String) Appsignal.internal_logger.warn( "Logger message was ignored, because it was not a String: #{message.inspect}" @@ -158,6 +165,10 @@ def silence(_severity = ERROR, &block) block.call end + def broadcast_to(logger) + @loggers << logger + end + private attr_reader :default_attributes, :appsignal_attributes diff --git a/spec/lib/appsignal/logger_spec.rb b/spec/lib/appsignal/logger_spec.rb index ac80dfe5..f75c2310 100644 --- a/spec/lib/appsignal/logger_spec.rb +++ b/spec/lib/appsignal/logger_spec.rb @@ -338,6 +338,52 @@ end end + describe "#broadcast_to" do + it "broadcasts the message to the given logger" do + other_device = StringIO.new + other_logger = ::Logger.new(other_device) + + logger.broadcast_to(other_logger) + + expect(Appsignal::Extension).to receive(:log) + .with("group", 3, 0, "Log message", instance_of(Appsignal::Extension::Data)) + + logger.info("Log message") + + expect(other_device.string).to include("INFO -- group: Log message") + end + + if DependencyHelper.rails_present? + describe "wrapped in ActiveSupport::TaggedLogging" do + let(:other_stream) { StringIO.new } + let(:other_logger) { ::Logger.new(other_stream) } + + let(:logger) do + appsignal_logger = Appsignal::Logger.new("group") + appsignal_logger.broadcast_to(other_logger) + ActiveSupport::TaggedLogging.new(appsignal_logger) + end + + it "broadcasts a tagged message to the given logger" do + expect(Appsignal::Extension).to receive(:log) + .with( + "group", + 3, + 0, + a_string_starting_with("[My tag] [My other tag] Some message"), + Appsignal::Utils::Data.generate({}) + ) + + logger.tagged("My tag", "My other tag") do + logger.info("Some message") + end + + expect(other_stream.string).to include("INFO -- group: [My tag] [My other tag] Some message") + end + end + end + end + [ ["debug", 2, ::Logger::INFO], ["info", 3, ::Logger::WARN],