Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MulticastLogger DEBUG mode #16990

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/vmdb/loggers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def self.create_loggers
private_class_method :create_loggers

def self.create_multicast_logger(log_file_path, logger_class = VMDBLogger)
MulticastLogger.new(logger_class.new(log_file_path)).tap do |l|
logger_instance = logger_class.new(log_file_path).tap do |logger|
logger.level = Logger::DEBUG
end
MulticastLogger.new(logger_instance).tap do |l|
l.loggers << $container_log if ENV["CONTAINER"]
end
end
Expand Down
54 changes: 54 additions & 0 deletions spec/lib/vmdb/loggers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
describe Vmdb::Loggers do
let(:log_file) { Rails.root.join("log", "foo.log").to_s }

def in_container_env(example)
old_env = ENV.delete('CONTAINER')
ENV['CONTAINER'] = 'true'
example.run
ensure
# ENV['x'] = nil deletes the key because ENV accepts only string values
ENV['CONTAINER'] = old_env
end

describe "#create_multicast_logger (private)" do
it "defaults the lower level loggers to `DEBUG`" do
log = described_class.send(:create_multicast_logger, log_file)

expect(log.loggers.first.level).to eq(Logger::DEBUG)
expect(log.loggers.to_a.size).to eq(1)
end

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

it "sets logger_instance and $container_log to debug" do
log = described_class.send(:create_multicast_logger, log_file)

expect(log.loggers.first.level).to eq(Logger::DEBUG)
expect(log.loggers.to_a.last.level).to eq(Logger::DEBUG)
end
end
end

describe "#apply_config_value (private)" do
it "will update the main lower level logger instance" do
log = described_class.send(:create_multicast_logger, log_file)
described_class.send(:apply_config_value, {:foo => :info}, log, :foo)

expect(log.loggers.first.level).to eq(Logger::INFO)
expect(log.loggers.to_a.size).to eq(1)
end

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

it "will always keep $container_log as DEBUG" do
log = described_class.send(:create_multicast_logger, log_file)
described_class.send(:apply_config_value, {:foo => :info}, log, :foo)

expect(log.loggers.first.level).to eq(Logger::INFO)
expect(log.loggers.to_a.last.level).to eq(Logger::DEBUG)
end
end
end
end