diff options
author | elijah <elijah@riseup.net> | 2013-02-27 23:53:10 -0800 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2013-02-27 23:53:10 -0800 |
commit | 56a42b9858058473a07f254c49d0bb00dadce2d9 (patch) | |
tree | 834f6371fddc17ccf7c4806a08f298d07f9b2ddf /lib | |
parent | 006744350595f89818e23ee77d096adf9b47207b (diff) | |
download | leap_cli-56a42b9858058473a07f254c49d0bb00dadce2d9.tar.gz leap_cli-56a42b9858058473a07f254c49d0bb00dadce2d9.tar.bz2 |
improve logging: set exit code on puppet error, better puppet error handling, better handling of multi-line puppet log entries.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/leap_cli/log.rb | 19 | ||||
-rw-r--r-- | lib/leap_cli/logger.rb | 47 |
2 files changed, 42 insertions, 24 deletions
diff --git a/lib/leap_cli/log.rb b/lib/leap_cli/log.rb index fb43d7b..4d5e4da 100644 --- a/lib/leap_cli/log.rb +++ b/lib/leap_cli/log.rb @@ -78,7 +78,7 @@ module LeapCli if title prefix_options = case title when :error then ['error', :red, :bold] - when :warning then ['warning', :yellow, :bold] + when :warning then ['warning:', :yellow, :bold] when :info then ['info', :cyan, :bold] when :updated then ['updated', :cyan, :bold] when :updating then ['updating', :cyan, :bold] @@ -126,6 +126,7 @@ module LeapCli # # Add a raw log entry, without any modifications (other than indent). # Content to be logged is yielded by the block. + # Block may be either a string or array of strings. # # if mode == :stdout, output is sent to STDOUT. # if mode == :log, output is sent to log file, if present. @@ -134,16 +135,18 @@ module LeapCli # NOTE: print message (using 'print' produces better results than 'puts' when multiple threads are logging) if mode == :log if LeapCli.log_output_stream - message = yield - if message + messages = [yield].compact.flatten + if messages.any? timestamp = Time.now.strftime("%b %d %H:%M:%S") - LeapCli.log_output_stream.print("#{timestamp} #{message}\n") + messages.each do |message| + LeapCli.log_output_stream.print("#{timestamp} #{message}\n") + end LeapCli.log_output_stream.flush end end elsif mode == :stdout - message = yield - if message + messages = [yield].compact.flatten + if messages.any? indent ||= LeapCli.indent_level indent_str = "" indent_str += " " * indent.to_i @@ -152,7 +155,9 @@ module LeapCli else indent_str += ' = ' end - STDOUT.print("#{indent_str}#{message}\n") + messages.each do |message| + STDOUT.print("#{indent_str}#{message}\n") + end end end end diff --git a/lib/leap_cli/logger.rb b/lib/leap_cli/logger.rb index eca7aa6..373af35 100644 --- a/lib/leap_cli/logger.rb +++ b/lib/leap_cli/logger.rb @@ -44,9 +44,9 @@ module LeapCli end def log(level, message, line_prefix=nil, options={}) - # in some cases, when the message doesn't end with a return, we buffer it and - # wait until we encounter the return before we log the message out. if message !~ /\n$/ && level <= 2 && line_prefix.is_a?(String) + # in some cases, when the message doesn't end with a return, we buffer it and + # wait until we encounter the return before we log the message out. @message_buffer ||= "" @message_buffer += message return @@ -56,26 +56,30 @@ module LeapCli end options[:level] ||= level - message.lines.each do |line| - [:stdout, :log].each do |mode| - LeapCli::log_raw(mode) { - formatted_line, formatted_prefix, line_options = apply_formatting(mode, line, line_prefix, options) - if line_options[:level] <= self.level && formatted_line && formatted_line.chars.any? - if formatted_prefix - "[#{formatted_prefix}] #{formatted_line}" - else - formatted_line - end - else - nil - end - } + [:stdout, :log].each do |mode| + LeapCli::log_raw(mode) do + message_lines(mode, message, line_prefix, options) end end end private + def message_lines(mode, message, line_prefix, options) + formatted_message, formatted_prefix, message_options = apply_formatting(mode, message, line_prefix, options) + if message_options[:level] <= self.level && formatted_message && formatted_message.chars.any? + if formatted_prefix + formatted_message.lines.collect { |line| + "[#{formatted_prefix}] #{line.sub(/\s+$/, '')}" + } + else + formatted_message.lines.collect {|line| line.sub(/\s+$/, '')} + end + else + nil + end + end + ## ## FORMATTING ## @@ -114,10 +118,15 @@ module LeapCli { :match => /^warning: .*is deprecated.*$/, :level => 2, :color => :yellow, :priority => -10}, { :match => /^warning: Scope.*$/, :level => 2, :color => :yellow, :priority => -10}, { :match => /^notice:/, :level => 1, :color => :cyan, :priority => -20}, - { :match => /^err:/, :level => 0, :color => :red, :priority => -20}, { :match => /^warning:/, :level => 0, :color => :yellow, :priority => -20}, { :match => /^Duplicate declaration:/, :level => 0, :color => :red, :priority => -20}, { :match => /Finished catalog run/, :level => 0, :color => :green, :priority => -10}, + + # PUPPET FATAL ERRORS + { :match => /^err:/, :level => 0, :color => :red, :priority => -1, :exit => 1}, + { :match => /^Failed to parse template/, :level => 0, :color => :red, :priority => -1, :exit => 1}, + { :match => /^Parameter matches failed:/, :level => 0, :color => :red, :priority => -1, :exit => 1}, + { :match => /^Syntax error/, :level => 0, :color => :red, :priority => -1, :exit => 1} ] def self.sorted_formatters @@ -164,6 +173,10 @@ module LeapCli message.replace(message + formatter[:append]) unless formatter[:append].nil? message.replace(Time.now.strftime('%Y-%m-%d %T') + ' ' + message) if formatter[:timestamp] + if formatter[:exit] + LeapCli::Util.exit_status(formatter[:exit]) + end + # stop formatting, unless formatter was just for string replacement break unless formatter[:replace] end |