Skip to content

Commit

Permalink
Return the raw logger unless logger_class is present
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Nov 28, 2023
1 parent b8aabc6 commit 811ea67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 51 deletions.
7 changes: 6 additions & 1 deletion lib/vmdb/loggers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.apply_config(config)
Vmdb::Plugins.each { |p| p.try(:apply_logger_config, config) }
end

def self.create_logger(log_file_name, logger_class = ManageIQ::Loggers::Base)
def self.create_logger(log_file_name, logger_class = nil)
if MiqEnvironment::Command.is_container?
create_container_logger(log_file_name, logger_class)
elsif MiqEnvironment::Command.supports_systemd?
Expand All @@ -52,6 +52,7 @@ def self.create_logger(log_file_name, logger_class = ManageIQ::Loggers::Base)
log_file = log_path_from_file(log_file)
progname = progname_from_file(log_file)

logger_class ||= ManageIQ::Loggers::Base
logger_class.new(log_file, :progname => progname)
end

Expand All @@ -62,6 +63,8 @@ def self.create_logger(log_file_name, logger_class = ManageIQ::Loggers::Base)
progname = progname_from_file(log_file_name)
logger.progname = progname

return logger if logger_class.nil?

create_wrapper_logger(progname, logger_class, logger)
end

Expand All @@ -72,6 +75,8 @@ def self.create_logger(log_file_name, logger_class = ManageIQ::Loggers::Base)
progname = progname_from_file(log_file_name)
logger.progname = progname

return logger if logger_class.nil?

create_wrapper_logger(progname, logger_class, logger)
end

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

subject { described_class.create_logger(log_file) }

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

before do
# Hide the container logger output to STDOUT
allow(container_log.logdev).to receive(:write) if container_log
end

it "responds to #<<" do
expect(subject).to respond_to(:<<)
end
Expand All @@ -64,11 +57,7 @@ def in_container_env(example)
end

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

describe "#datetime_format" do
Expand All @@ -90,20 +79,8 @@ def in_container_env(example)
subject.level = old_level
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

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")
else
expect(subject.logdev).not_to receive(:write).with("test message")
end
expect(subject.logdev).not_to receive(:write).with("test message")

subject.debug("test message")
end
Expand All @@ -128,8 +105,7 @@ def in_container_env(example)

context "#<<" do
it "forwards to the other loggers" do
expect(subject).to receive(:<<).with("test message").and_call_original
expect(container_log).to receive(:<<).with("test message").and_call_original if container_log
expect(subject).to receive(:<<).with("test message")#.and_call_original

subject << "test message"
end
Expand All @@ -139,12 +115,9 @@ 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
expect(subject).to receive(:add).with(1, nil, "test message")

subject.info("test message")

expect(log_file.string).to include("test message") unless container_log
end
end

Expand All @@ -154,12 +127,9 @@ 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
expect(subject).to receive(:add).with(1, nil, "test message")

subject.info("test message")

expect(log_file.read).to include("test message") unless container_log
end
end

Expand All @@ -169,31 +139,20 @@ 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
expect(subject).to receive(:add).with(1, nil, "test message")

subject.info("test message")

expect(File.read(log_file)).to include("test message") unless container_log
end
end
end

context "in a non-container environment" do
it "does not have a container logger" do
expect(container_log).to be_nil
end

include_examples "has basic logging functionality"
end

context "in a container environment" do
around { |example| in_container_env(example) }

it "has a container logger" do
expect(container_log).to_not be_nil
end

include_examples "has basic logging functionality"
end
end
Expand All @@ -212,12 +171,10 @@ 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

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)
expect(log.level).to eq(Logger::ERROR)
end
end
end
Expand Down

0 comments on commit 811ea67

Please sign in to comment.