Skip to content

Commit

Permalink
Add a wrap method to support BroadcastLogger/ActiveSupport::Logger.br…
Browse files Browse the repository at this point in the history
…oadcast
  • Loading branch information
agrare committed Sep 26, 2024
1 parent 4f741e4 commit 118736c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
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
4 changes: 2 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,7 @@ 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)) }
super(cloud_watch_logdev).tap { |logger| logger.wrap(container_logger) }
end

def initialize(logdev, *args)
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
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

0 comments on commit 118736c

Please sign in to comment.