From 73db44dfc326687d9e07ad849f0387382c5fb409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Lu=CC=88dicke?= Date: Wed, 14 Sep 2016 11:13:56 +0200 Subject: [PATCH 1/2] colorize LogMessage level --- CHANGELOG.md | 1 + lib/airbrussh/console_formatter.rb | 15 ++++++++++++++- test/airbrussh/formatter_test.rb | 10 +++++----- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96eb9ae..b813947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. This projec ## [Unreleased] +* colorize LogMessage label on WARN level and above ([@klyonrad](https://github.com/klyonrad)) * Your contribution here! ## [1.1.2][] (2017-01-02) diff --git a/lib/airbrussh/console_formatter.rb b/lib/airbrussh/console_formatter.rb index a7e21fa..3b1cd51 100644 --- a/lib/airbrussh/console_formatter.rb +++ b/lib/airbrussh/console_formatter.rb @@ -69,7 +69,20 @@ def write(obj) def write_log_message(log_message) return if debug?(log_message) print_task_if_changed - print_indented_line(log_message.to_s) + print_indented_line(format_log_message(log_message)) + end + + def format_log_message(log_message) + case log_message.verbosity + when SSHKit::Logger::WARN + "#{yellow('WARN')} #{log_message}" + when SSHKit::Logger::ERROR + "#{red('ERROR')} #{log_message}" + when SSHKit::Logger::FATAL + "#{red('FATAL')} #{log_message}" + else + log_message.to_s + end end # For SSHKit versions up to and including 1.7.1, the stdout and stderr diff --git a/test/airbrussh/formatter_test.rb b/test/airbrussh/formatter_test.rb index 7a5e480..158c27b 100644 --- a/test/airbrussh/formatter_test.rb +++ b/test/airbrussh/formatter_test.rb @@ -245,7 +245,7 @@ def test_handles_rake_tasks " 02 command 2\n", / ✔ 02 #{@user_at_localhost} \d.\d+s\n/, "00:00 special_rake_task_2\n", - " New task starting\n", + " #{red('ERROR')} New task starting\n", "00:00 special_rake_task_3\n", " 01 echo command 3\n", " 01 command 3\n", @@ -253,7 +253,7 @@ def test_handles_rake_tasks " 02 echo command 4\n", " 02 command 4\n", / ✔ 02 #{@user_at_localhost} \d.\d+s\n/, - " All done\n" + " #{red('WARN')} All done\n" ) assert_log_file_lines( @@ -280,9 +280,9 @@ def test_log_message_levels assert_output_lines( " Test\n", - " Test\n", - " Test\n", - " Test\n", + " #{red('FATAL')} Test\n", + " #{red('ERROR')} Test\n", + " #{yellow('WARN')} Test\n", " Test\n" ) From 3dd45fc89a0a4966035d420add55829c481724d1 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Tue, 11 Apr 2017 07:14:55 -0700 Subject: [PATCH 2/2] Fix color-related test failures The `red`, `yellow`, etc. helpers within `formatter_test.rb` are specifically used only for testing logfile output, not console output. Since color is not present in logfile output for SSHKit > 1.7.1, these helpers essentially do nothing in the latest SSHKit. Therefore, when asserting color console output, it is necessary to use the actual ANSI color control codes, as this commit does in the `test_log_message_levels`. Note that tests that require color must explicitly turn on color using `airbrussh_config.color = true`. I removed the color expectations from `test_handles_rake_tasks`, since color is not enabled for that test. --- test/airbrussh/formatter_test.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/airbrussh/formatter_test.rb b/test/airbrussh/formatter_test.rb index 158c27b..2a1d4d6 100644 --- a/test/airbrussh/formatter_test.rb +++ b/test/airbrussh/formatter_test.rb @@ -245,7 +245,7 @@ def test_handles_rake_tasks " 02 command 2\n", / ✔ 02 #{@user_at_localhost} \d.\d+s\n/, "00:00 special_rake_task_2\n", - " #{red('ERROR')} New task starting\n", + " ERROR New task starting\n", "00:00 special_rake_task_3\n", " 01 echo command 3\n", " 01 command 3\n", @@ -253,7 +253,7 @@ def test_handles_rake_tasks " 02 echo command 4\n", " 02 command 4\n", / ✔ 02 #{@user_at_localhost} \d.\d+s\n/, - " #{red('WARN')} All done\n" + " WARN All done\n" ) assert_log_file_lines( @@ -268,7 +268,8 @@ def test_handles_rake_tasks end def test_log_message_levels - configure do |_airbrussh_config, sshkit_config| + configure do |airbrussh_config, sshkit_config| + airbrussh_config.color = true sshkit_config.output_verbosity = Logger::DEBUG end @@ -280,9 +281,9 @@ def test_log_message_levels assert_output_lines( " Test\n", - " #{red('FATAL')} Test\n", - " #{red('ERROR')} Test\n", - " #{yellow('WARN')} Test\n", + " \e[0;31;49mFATAL\e[0m Test\n", + " \e[0;31;49mERROR\e[0m Test\n", + " \e[0;33;49mWARN\e[0m Test\n", " Test\n" )