Skip to content

Commit

Permalink
Fix Logger subclasses providing no default logdev
Browse files Browse the repository at this point in the history
Logger.new requires a log device as the first argument.

Normalized the other loggers to match the Logger#initialize interface for consistency.

Fixes the following bug:

irb(main):001> ManageIQ::Loggers::Base.new
xxx/.rubies/ruby-3.3.6/lib/ruby/3.3.0/logger.rb:578:in `initialize': wrong number of arguments (given 0, expected 1..3) (ArgumentError)
	from manageiq-loggers (1.2.0) lib/manageiq/loggers/base.rb:27:in `initialize'

irb(main):002> ManageIQ::Loggers::JSONLogger.new
xxx/.rubies/ruby-3.3.6/lib/ruby/3.3.0/logger.rb:578:in `initialize': wrong number of arguments (given 0, expected 1..3) (ArgumentError)
	from manageiq-loggers (1.2.0) lib/manageiq/loggers/base.rb:27:in `initialize'
	from manageiq-loggers (1.2.0) lib/manageiq/loggers/json_logger.rb:5:in `initialize'
  • Loading branch information
jrafanie committed Feb 4, 2025
1 parent a49940f commit 37f6735
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/manageiq/loggers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def wrap(source, *loggers)
end
end

def initialize(*_, **_)
def initialize(_logdev = nil, *_, **_)
super
self.level = INFO

Expand Down
2 changes: 1 addition & 1 deletion lib/manageiq/loggers/json_logger.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ManageIQ
module Loggers
class JSONLogger < Base
def initialize(*_, **_)
def initialize(_logdev = nil, *_, **_)
super
self.formatter = Formatter.new
end
Expand Down
4 changes: 4 additions & 0 deletions spec/manageiq/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
described_class.instance_variable_set("@log_hashes_filter", nil)
end

it "provides nil default log device" do
expect(described_class.new.logdev).to eq nil
end

it "logs a message" do
logger.info(message)

Expand Down
4 changes: 4 additions & 0 deletions spec/manageiq/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
let(:logger) { described_class.new }
let(:message) { "testing 1, 2, 3" }

it "provides a default log device" do
expect(described_class.new.logdev).to be_kind_of(Logger::LogDevice)
end

it "writes to STDOUT by default" do
expect { logger.info(message) }.to output.to_stdout
end
Expand Down
4 changes: 4 additions & 0 deletions spec/manageiq/journald_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
RSpec.describe ManageIQ::Loggers::Journald, :linux do
let(:logger) { described_class.new }

it "provides nil default log device" do
expect(described_class.new.logdev).to eq nil
end

context "progname" do
it "sets the progname to manageiq by default" do
expect(logger.progname).to eq("manageiq")
Expand Down
4 changes: 4 additions & 0 deletions spec/manageiq/json_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
let(:logger) { described_class.new(buffer) }
let(:message) { "testing 1, 2, 3" }

it "provides nil default log device" do
expect(described_class.new.logdev).to eq nil
end

it "logs a message in JSON format" do
logger.info(message)

Expand Down

0 comments on commit 37f6735

Please sign in to comment.