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

Colorize LogMessages again #94

Closed
klyonrad opened this issue Sep 14, 2016 · 3 comments
Closed

Colorize LogMessages again #94

klyonrad opened this issue Sep 14, 2016 · 3 comments

Comments

@klyonrad
Copy link

klyonrad commented Sep 14, 2016

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

@mattbrictson
Copy link
Owner

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.

@klyonrad
Copy link
Author

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

@mattbrictson
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants