Skip to content

Commit

Permalink
Use new public 7.1 broadcast logging API
Browse files Browse the repository at this point in the history
We no longer need to wrap loggers as you can inspect
all broadcasted loggers and automatically send the same
log level or other method calls to each.

See: https://www.github.com/rails/rails/pull/48615
  • Loading branch information
jrafanie committed Feb 13, 2025
1 parent d5d839d commit e96f048
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
19 changes: 2 additions & 17 deletions lib/vmdb/loggers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,8 @@ def self.create_logger(log_file_name, logger_class = ManageIQ::Loggers::Base)
log_file = ManageIQ.root.join("log", log_file) if log_file.try(:dirname).to_s == "."
progname = log_file.try(:basename, ".*").to_s

logger_class.new(nil, :progname => progname).tap do |logger|
# HACK: In order to access the wrapped logger in test, we inject it as an instance var.
if Rails.env.test?
logger.instance_variable_set(:@wrapped_logger, wrapped_logger)

def logger.wrapped_logger
@wrapped_logger
end
end

logger.extend(ActiveSupport::Logger.broadcast(wrapped_logger))
if logger.class.const_defined?(:FormatterMixin)
wrapped_logger.formatter.extend(logger.class.const_get(:FormatterMixin))
end

wrapped_logger.progname = progname
end
logger = logger_class.new(nil, :progname => progname)
ActiveSupport::BroadcastLogger.new(logger, wrapped_logger)
end

private_class_method def self.configure_external_loggers
Expand Down
57 changes: 40 additions & 17 deletions spec/lib/vmdb/loggers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def in_container_env(example)

subject { described_class.create_logger(log_file) }

let(:container_log) { subject.try(:wrapped_logger) }
let(:container_log) { subject.try(:broadcasts).try(:last) }

before do
# Hide the container logger output to STDOUT
Expand Down Expand Up @@ -65,15 +65,21 @@ def in_container_env(example)

it "#logdev" do
if container_log
expect(subject.logdev).to be_nil
expect(subject.broadcasts.first.logdev).to be_nil
expect(subject.broadcasts.last.logdev).to be_a Logger::LogDevice
else
expect(subject.logdev).to be_a Logger::LogDevice
end
end

describe "#datetime_format" do
it "return nil" do
expect(subject.datetime_format).to be nil
if container_log
expect(subject.datetime_format.first).to be nil
expect(subject.datetime_format.last).to be nil
else
expect(subject.datetime_format).to be nil
end
end

it "does not raise an error" do
Expand All @@ -91,16 +97,19 @@ def in_container_env(example)
end

it "forwards to the other loggers" do
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
expect(container_log).to receive(:add).with(1, nil, "test message").and_call_original if container_log

if container_log
expect(subject.broadcasts.first).to receive(:add).with(1, nil, "test message").and_call_original
expect(subject.broadcasts.last).to receive(:add).with(1, nil, "test message").and_call_original
else
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
end
subject.info("test message")
end

it "only forwards the message if the severity is correct" do
if container_log
expect(subject.logdev).to be_nil
expect(container_log.logdev).not_to receive(:write).with("test message")
expect(subject.broadcasts.first.logdev).to be_nil
expect(subject.broadcasts.last.logdev).not_to receive(:write).with("test message")
else
expect(subject.logdev).not_to receive(:write).with("test message")
end
Expand Down Expand Up @@ -139,8 +148,12 @@ def in_container_env(example)
let(:log_file) { StringIO.new }

it "logs correctly" do
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
expect(container_log).to receive(:add).with(1, nil, "test message").and_call_original if container_log
if container_log
expect(subject.broadcasts.first).to receive(:add).with(1, nil, "test message").and_call_original
expect(subject.broadcasts.last).to receive(:add).with(1, nil, "test message").and_call_original
else
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
end

subject.info("test message")

Expand All @@ -154,8 +167,12 @@ def in_container_env(example)
after { log_file.delete if log_file.exist? }

it "logs correctly" do
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
expect(container_log).to receive(:add).with(1, nil, "test message").and_call_original if container_log
if container_log
expect(subject.broadcasts.first).to receive(:add).with(1, nil, "test message").and_call_original
expect(subject.broadcasts.last).to receive(:add).with(1, nil, "test message").and_call_original
else
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
end

subject.info("test message")

Expand All @@ -169,9 +186,12 @@ def in_container_env(example)
after { File.delete(log_file) if File.exist?(log_file) }

it "logs correctly" do
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
expect(container_log).to receive(:add).with(1, nil, "test message").and_call_original if container_log

if container_log
expect(subject.broadcasts.first).to receive(:add).with(1, nil, "test message").and_call_original
expect(subject.broadcasts.last).to receive(:add).with(1, nil, "test message").and_call_original
else
expect(subject).to receive(:add).with(1, nil, "test message").and_call_original
end
subject.info("test message")

expect(File.read(log_file)).to include("test message") unless container_log
Expand Down Expand Up @@ -212,12 +232,15 @@ def in_container_env(example)

it "will honor the log level in the container logger" do
log = described_class.create_logger(log_file_name)
container_log = log.wrapped_logger
container_log = log.broadcasts.last

described_class.apply_config_value({:level_foo => :error}, log, :level_foo)

expect(log.level).to eq(Logger::ERROR)
expect(container_log.level).to eq(Logger::ERROR)

described_class.apply_config_value({:level_foo => :debug}, log, :level_foo)
expect(log.level).to eq(Logger::DEBUG)
expect(container_log.level).to eq(Logger::DEBUG)
end
end
end
Expand Down

0 comments on commit e96f048

Please sign in to comment.