Skip to content

Commit

Permalink
(BOLT-523) Proxy r10k logs to the bolt logger
Browse files Browse the repository at this point in the history
This introduces a proxy outputter for log4r that redirects all the log
messages back to the bolt logger, where they can be sorted and handled
in the standard way.
  • Loading branch information
nicklewis committed Jul 19, 2018
1 parent 93d691b commit 2b04550
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/bolt/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'bolt/outputter'
require 'bolt/puppetdb'
require 'bolt/pal'
require 'bolt/r10k_log_proxy'
require 'bolt/target'
require 'bolt/version'

Expand Down Expand Up @@ -370,8 +371,8 @@ def install_puppetfile(puppetfile, modulepath)
}
install_action = R10K::Action::Puppetfile::Install.new(r10k_config, nil)

# Override the r10k logger with our own logger
install_action.instance_variable_set(:@logger, Logging.logger[install_action])
# Override the r10k logger with a proxy to our own logger
R10K::Logging.instance_variable_set(:@outputter, Bolt::R10KLogProxy.new)

ok = install_action.call
outputter.print_puppetfile_result(ok, puppetfile, moduledir)
Expand Down
30 changes: 30 additions & 0 deletions lib/bolt/r10k_log_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'log4r/outputter/outputter'

module Bolt
class R10KLogProxy < Log4r::Outputter
def initialize
super('bolt')

@logger = Logging.logger[self]
end

def canonical_log(event)
level = to_bolt_level(event.level)
@logger.send(level, event.data)
end

# Convert an r10k log level to a bolt log level. These correspond 1-to-1
# except that r10k has debug, debug1, and debug2. The log event has the log
# level as an integer that we need to look up.
def to_bolt_level(level_num)
level_str = Log4r::LNAMES[level_num]&.downcase || 'debug'
if level_str =~ /debug/
:debug
else
level_str.to_sym
end
end
end
end

0 comments on commit 2b04550

Please sign in to comment.