Skip to content

Commit 8ed0882

Browse files
authored
Merge pull request #567 from jrafanie/rails71-broadcast-logger
Wrap all automate logger calls to process the request_id
2 parents 1d43f73 + 911b05e commit 8ed0882

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

lib/manageiq/automation_engine/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def self.plugin_name
1919
def self.init_loggers
2020
# This require avoids autoload during rails boot
2121
require 'manageiq/automation_engine/logger'
22-
$miq_ae_logger ||= Vmdb::Loggers.create_logger("automation.log", ManageIQ::AutomationEngine::Logger)
22+
$miq_ae_logger ||= ManageIQ::AutomationEngine::Logger.create_log_wrapper
2323
end
2424

2525
def self.apply_logger_config(config)

lib/manageiq/automation_engine/logger.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
module ManageIQ
22
module AutomationEngine
33
class Logger < ManageIQ::Loggers::Base
4+
attr_reader :automation_log_wrapper
5+
6+
def initialize(*args, automation_log_wrapper:, **kwargs)
7+
@automation_log_wrapper = automation_log_wrapper
8+
super(*args, **kwargs)
9+
end
10+
11+
def self.create_log_wrapper(io = File::NULL)
12+
# We modify the interface of logger methods such as info/warn/etc. to allow the keyword argument
13+
# resource_id. Therefore, we need to wrap all client logger calls to these methods to process the resource_id,
14+
# cut the request_log entry and forward the remaining arguments to the logger.
15+
new(io, :progname => "automation", :automation_log_wrapper => Vmdb::Loggers.create_logger("automation.log"))
16+
end
17+
418
private def add_to_db(severity, message = nil, progname = nil, resource_id: nil)
519
return [severity, message, progname] unless resource_id
620

@@ -28,31 +42,37 @@ class Logger < ManageIQ::Loggers::Base
2842

2943
def info(progname = nil, resource_id: nil, &block)
3044
severity, message, progname = add_to_db(INFO, nil, progname, resource_id: resource_id, &block)
45+
automation_log_wrapper.add(severity, message, progname, &block)
3146
add(severity, message, progname, &block)
3247
end
3348

3449
def debug(progname = nil, resource_id: nil, &block)
3550
severity, message, progname = add_to_db(DEBUG, nil, progname, resource_id: resource_id, &block)
51+
automation_log_wrapper.add(severity, message, progname, &block)
3652
add(severity, message, progname, &block)
3753
end
3854

3955
def warn(progname = nil, resource_id: nil, &block)
4056
severity, message, progname = add_to_db(WARN, nil, progname, resource_id: resource_id, &block)
57+
automation_log_wrapper.add(severity, message, progname, &block)
4158
add(severity, message, progname, &block)
4259
end
4360

4461
def error(progname = nil, resource_id: nil, &block)
4562
severity, message, progname = add_to_db(ERROR, nil, progname, resource_id: resource_id, &block)
63+
automation_log_wrapper.add(severity, message, progname, &block)
4664
add(severity, message, progname, &block)
4765
end
4866

4967
def fatal(progname = nil, resource_id: nil, &block)
5068
severity, message, progname = add_to_db(FATAL, nil, progname, resource_id: resource_id, &block)
69+
automation_log_wrapper.add(severity, message, progname, &block)
5170
add(severity, message, progname, &block)
5271
end
5372

5473
def unknown(progname = nil, resource_id: nil, &block)
5574
severity, message, progname = add_to_db(UNKNOWN, nil, progname, resource_id: resource_id, &block)
75+
automation_log_wrapper.add(severity, message, progname, &block)
5676
add(severity, message, progname, &block)
5777
end
5878
end

spec/lib/manageiq/automation_engine/logger_spec.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe ManageIQ::AutomationEngine::Logger do
22
let(:io) { StringIO.new }
33
subject do
4-
described_class.new(io).tap do |logger|
4+
described_class.create_log_wrapper(io).tap do |logger|
55
logger.level = Logger::DEBUG
66
end
77
end
@@ -70,8 +70,15 @@
7070
end
7171

7272
describe "supports container logging" do
73-
subject { Vmdb::Loggers.create_logger("automation.log", described_class) }
74-
let(:container_log) { subject.try(:wrapped_logger) }
73+
subject { described_class.create_log_wrapper }
74+
let(:automation_log_wrapper) { subject.automation_log_wrapper }
75+
let(:container_log) do
76+
if automation_log_wrapper.respond_to?(:broadcasts)
77+
automation_log_wrapper.broadcasts.last
78+
else
79+
automation_log_wrapper.wrapped_logger
80+
end
81+
end
7582

7683
before do
7784
stub_const("ENV", ENV.to_h.merge("CONTAINER" => "true"))
@@ -120,7 +127,7 @@
120127

121128
it "without a resource_id and with a block" do
122129
expect(subject.logdev).to be_nil # i.e. won't write to a file
123-
expect(subject).to receive(:add).with(Logger::INFO, nil, nil).and_call_original
130+
expect(subject).to receive(:add).with(Logger::INFO, nil, nil).and_call_original
124131
expect(container_log).to receive(:add).with(Logger::INFO, nil, nil).and_call_original
125132
expect(container_log.logdev).to receive(:write).with(/"message":"foo"/)
126133

0 commit comments

Comments
 (0)