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

Add a wrap method to support BroadcastLogger/ActiveSupport::Logger.broadcast #63

Merged
merged 1 commit into from
Sep 30, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
rails-version:
- '6.1'
- '7.0'
- '7.1'
env:
TEST_RAILS_VERSION: ${{ matrix.rails-version }}
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ minimum_version =
case ENV['TEST_RAILS_VERSION']
when "6.1"
"~>6.1.7"
else
when "7.0"
"~>7.0.8"
else
"~>7.1.4"
end

gem "activesupport", minimum_version
17 changes: 17 additions & 0 deletions lib/manageiq/loggers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ module Loggers
class Base < Logger
MAX_LOG_LINE_LENGTH = 8.kilobytes

class << self
def wrap(source, *loggers)
loggers.flatten!
if ActiveSupport.gem_version >= Gem::Version.new("7.1.0")
require 'active_support/broadcast_logger'
ActiveSupport::BroadcastLogger.new(*loggers.unshift(source))
else
loggers.each { |logger| source.extend(ActiveSupport::Logger.broadcast(logger)) }
source
end
end
end

def initialize(*_, **_)
super
self.level = INFO
Expand All @@ -28,6 +41,10 @@ def initialize(*_, **_)
@local_levels = {}
end

def wrap(loggers)
self.class.wrap(self, loggers)
end

# Silences the logger for the duration of the block.
#
# Taken from activesupport/logger_silence
Expand Down
5 changes: 3 additions & 2 deletions lib/manageiq/loggers/cloud_watch.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'active_support'
require 'active_support/core_ext/string'
require 'active_support/core_ext/object'
require 'active_support/logger'

module ManageIQ
Expand All @@ -20,7 +20,8 @@ def self.new(*args, access_key_id: nil, secret_access_key: nil, log_group: nil,

creds = {:access_key_id => access_key_id, :secret_access_key => secret_access_key}
cloud_watch_logdev = CloudWatchLogger::Client.new(creds, log_group, log_stream)
super(cloud_watch_logdev).tap { |logger| logger.extend(ActiveSupport::Logger.broadcast(container_logger)) }
cloud_watch_logger = super(cloud_watch_logdev)
cloud_watch_logger.wrap(container_logger)
end

def initialize(logdev, *args)
Expand Down
8 changes: 7 additions & 1 deletion lib/manageiq/loggers/journald.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ def add(severity, message = nil, progname = nil)
end

message = formatter.call(format_severity(severity), nil, progname, message)
caller_object = caller_locations(3, 1).first

# The call stack is different depending on if we are using the newer
# ActiveSupport::BroadcastLogger or the older ActiveSupport::Logger.broadcast
# so we have to account for that difference when walking up the caller_locations
# to get the "real" logging location.
callstack_start = ActiveSupport.gem_version >= Gem::Version.new("7.1.0") ? 7 : 3
caller_object = caller_locations(callstack_start, 1).first

Systemd::Journal.message(
:message => message,
Expand Down
2 changes: 1 addition & 1 deletion manageiq-loggers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency "activesupport", ">= 5.0", "< 7.1"
spec.add_runtime_dependency "activesupport", ">= 5.0"
spec.add_runtime_dependency "manageiq-password", "< 2"

spec.add_development_dependency "bundler"
Expand Down
8 changes: 0 additions & 8 deletions spec/manageiq/cloud_watch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
expect(CloudWatchLogger::Client::AWS_SDK::DeliveryThreadManager).to receive(:new).and_return(double("CloudWatchLogger::Client::AWS_SDK::DeliveryThreadManager", :deliver => nil))
end

it "returns a CloudWatch::Client" do
expect(described_class.new).to be_kind_of(ManageIQ::Loggers::CloudWatch)
end

it "the Container logger also receives the same messages" do
container_logger = ManageIQ::Loggers::Container.new
expect(ManageIQ::Loggers::Container).to receive(:new).and_return(container_logger)
Expand All @@ -53,10 +49,6 @@
expect(CloudWatchLogger::Client::AWS_SDK::DeliveryThreadManager).to receive(:new).and_return(double("CloudWatchLogger::Client::AWS_SDK::DeliveryThreadManager", :deliver => nil))
end

it "returns a CloudWatch::Client" do
expect(described_class.new(**params)).to be_kind_of(ManageIQ::Loggers::CloudWatch)
end

it "the Container logger also receives the same messages" do
container_logger = ManageIQ::Loggers::Container.new
expect(ManageIQ::Loggers::Container).to receive(:new).and_return(container_logger)
Expand Down
3 changes: 1 addition & 2 deletions spec/manageiq/journald_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

context "code_file" do
it "sets the code_file" do
log = Logger.new(IO::NULL)
log.extend(ActiveSupport::Logger.broadcast(logger))
log = logger.wrap(Logger.new(IO::NULL))

expect(Systemd::Journal).to receive(:message).with(hash_including(:code_file => __FILE__, :code_line => __LINE__ + 1))
log.info("abcd") # NOTE this has to be exactly beneath the exect for the __LINE__ + 1 to work
Expand Down