From 811ea6707c373dcabb5d242f1bb832da5cb59dc8 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 28 Nov 2023 14:29:22 -0500 Subject: [PATCH] Return the raw logger unless logger_class is present --- lib/vmdb/loggers.rb | 7 ++++- spec/lib/vmdb/loggers_spec.rb | 57 +++++------------------------------ 2 files changed, 13 insertions(+), 51 deletions(-) diff --git a/lib/vmdb/loggers.rb b/lib/vmdb/loggers.rb index b19cc4574ce..14c4ee2ff3e 100644 --- a/lib/vmdb/loggers.rb +++ b/lib/vmdb/loggers.rb @@ -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? @@ -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 @@ -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 @@ -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 diff --git a/spec/lib/vmdb/loggers_spec.rb b/spec/lib/vmdb/loggers_spec.rb index 53153d9ae28..8525924d2cf 100644 --- a/spec/lib/vmdb/loggers_spec.rb +++ b/spec/lib/vmdb/loggers_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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