mattbrictson/airbrussh

Colorize LogMessages again

klyonrad opened this issue · 3 comments

With the old pretty formatter, the Message Level (fatal, error, warn, info...) was colorized. That made it quite easy for the human eye to see where the error messages are.

I wrote an informal patch that overrides the write_log_message method in CommandFormatter to restore the old colorize behaviour. It combines code from SSHKIT ( https://github.com/capistrano/sshkit/blob/v1.11.2/lib/sshkit/formatters/pretty.rb#L44 ) with the Airbrussh::Colors method.

Would you like a Pull Request for this change?

require 'airbrussh'

module Airbrussh
  class ConsoleFormatter < SSHKit::Formatter::Abstract
    include Airbrussh::Colors

    LEVEL_NAMES = %w{ DEBUG INFO WARN ERROR FATAL }.freeze
    LEVEL_COLORS = [:gray, :blue, :yellow, :red, :red].freeze # pretty formatter had :black, airbrussh colors has gray

    private

    def write_log_message(log_message)
      return if debug?(log_message)
      print_task_if_changed
      verbosity = log_message.verbosity
      # level = colorize(LEVEL_NAMES[verbosity].rjust(6), LEVEL_COLORS[verbosity])
      level = LEVEL_NAMES[verbosity].ljust(6)
      log_string = send(LEVEL_COLORS[verbosity], level + log_message.to_s)
      print_indented_line(log_string)
    end
  end
end

This change is also available in my fork in the branch colorize-logmessage https://github.com/klyonrad/airbrussh/tree/colorize-logmessage

Thanks for the idea! Could you show me a screenshot of some Airbrussh output that shows what your changes look like? I'm particularly interested in examples of error messages you are colorizing.

If you are referring to stderr command output, I don't want to color stderr red because stderr is not necessary error messages. Often times it is just debug messages or progress indication. Colorizing those in red would give the user the mistaken impression that something has gone wrong. This was one problem in Capistrano's pretty output that Airbrussh was specifically designed to correct.

Alright, here is how it looks (OS X Terminal):
airbrush_colors

This is with the code from above, in my Commit (https://github.com/klyonrad/airbrussh/commit/8b57a477cdc555fae6b3b571e1136cc92da87c98) I only colorized the verbosity Label, which makes more sense.

If you are referring to stderr command output, I don't want to color stderr red because stderr

I didn't check for that, but judging from the Code the write_log_message method is only used for LogMessage instances

    def write(obj)
      case obj
      when SSHKit::Command
        log_command_start(obj)
        log_and_clear_command_output(obj, :stderr)
        log_and_clear_command_output(obj, :stdout)
        log_command_exit(obj) if obj.finished?
      when SSHKit::LogMessage
        write_log_message(obj)
      end
    end

Thanks for the screenshot. I'm willing to accept a PR that adds a colorized WARN, ERROR or FATAL prefix for those levels (leaving the log message itself un-colorized). No prefix or color for INFO. Will that still work for you?