From c01cf5902b6981963a05c3c9c1da9f810639a26c Mon Sep 17 00:00:00 2001 From: Andrey Teplyashin Date: Sun, 9 Nov 2014 14:04:05 +0600 Subject: [PATCH] Add colored logs --- THANKS | 1 + ebin/rebar.app | 3 +++ inttest/logging/logging_rt.erl | 31 ++++++++++++++++++++++++------- src/rebar_log.erl | 29 ++++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/THANKS b/THANKS index 3db3b4c..1fcc531 100644 --- a/THANKS +++ b/THANKS @@ -127,3 +127,4 @@ alisdair sullivan Alexander Verbitsky Andras Horvath Drew Varner +Andrey Teplyashin diff --git a/ebin/rebar.app b/ebin/rebar.app index 55454e9..12208e2 100644 --- a/ebin/rebar.app +++ b/ebin/rebar.app @@ -54,6 +54,9 @@ %% Default log level {log_level, warn}, + %% Log colored + {log_colored, true}, + %% any_dir processing modules {any_dir_modules, [ rebar_require_vsn, diff --git a/inttest/logging/logging_rt.erl b/inttest/logging/logging_rt.erl index 2b8e54b..d086892 100644 --- a/inttest/logging/logging_rt.erl +++ b/inttest/logging/logging_rt.erl @@ -14,23 +14,40 @@ run(_Dir) -> SharedExpected = "==> logging_rt \\(compile\\)", %% provoke ERROR due to an invalid app file retest:log(info, "Check 'compile' failure output~n"), + {ERROR, WARN, INFO, DEBUG} = + case application:get_env(rebar, log_colored) of + {ok, true} -> + { + "\\e\\[1m\\e\\[31mERROR: \\e\\[0m", + "\\e\\[33mWARN: \\e\\[0m", + "\\e\\[32mINFO: \\e\\[0m", + "\\e\\[34mDEBUG: \\e\\[0m" + }; + _ -> + { + "ERROR: ", + "WARN: ", + "INFO: ", + "DEBUG: " + } + end, ok = check_output("./rebar compile -q", should_fail, - [SharedExpected, "ERROR: "], - ["WARN: ", "INFO: ", "DEBUG: "]), + [SharedExpected, ERROR], + [WARN, INFO, DEBUG]), %% fix bad app file ok = file:write_file(?APP_FILE, app(logging, [])), retest:log(info, "Check 'compile' success output~n"), ok = check_output("./rebar compile", should_succeed, [SharedExpected], - ["ERROR: ", "WARN: ", "INFO: ", "DEBUG: "]), + [ERROR, WARN, INFO, DEBUG]), retest:log(info, "Check 'compile -v' success output~n"), ok = check_output("./rebar compile -v", should_succeed, [SharedExpected], - ["ERROR: ", "INFO: ", "DEBUG: "]), + [ERROR, INFO, DEBUG]), retest:log(info, "Check 'compile -vv' success output~n"), ok = check_output("./rebar compile -vv", should_succeed, - [SharedExpected, "DEBUG: "], - ["ERROR: ", "INFO: "]), + [SharedExpected, DEBUG], + [ERROR, INFO]), ok. check_output(Cmd, FailureMode, Expected, Unexpected) -> @@ -47,7 +64,7 @@ check_output(Cmd, FailureMode, Expected, Unexpected) -> end. check_output1(Cmd, Captured, Expected, Unexpected) -> - ReOpts = [{capture, all, list}], + ReOpts = [{capture, all, list}, unicode], ExMatches = lists:zf( fun(Pattern) -> diff --git a/src/rebar_log.erl b/src/rebar_log.erl index ba25332..689e628 100644 --- a/src/rebar_log.erl +++ b/src/rebar_log.erl @@ -60,9 +60,10 @@ log(Level, Str, Args) -> log(Device, Level, Str, Args) -> {ok, LogLevel} = application:get_env(rebar, log_level), + {ok, LogColored} = application:get_env(rebar, log_colored), case should_log(LogLevel, Level) of true -> - io:format(Device, log_prefix(Level) ++ Str, Args); + io:format(Device, log_prefix(Level, LogColored) ++ Str, Args); false -> ok end. @@ -90,7 +91,33 @@ should_log(error, error) -> true; should_log(error, _) -> false; should_log(_, _) -> false. +log_prefix(Level, _Colored = false) -> + log_prefix(Level); +log_prefix(Level, _Colored = true) -> + color_from_level(Level) ++ log_prefix(Level) ++ reset_color(). + log_prefix(debug) -> "DEBUG: "; log_prefix(info) -> "INFO: "; log_prefix(warn) -> "WARN: "; log_prefix(error) -> "ERROR: ". + +color_from_level(debug) -> + color_foreground(blue); +color_from_level(info) -> + color_foreground(green); +color_from_level(warn) -> + color_foreground(yellow); +color_from_level(error) -> + color_bold() ++ color_foreground(red). + +color_foreground(black) -> "\e[30m"; +color_foreground(red) -> "\e[31m"; +color_foreground(green) -> "\e[32m"; +color_foreground(yellow) -> "\e[33m"; +color_foreground(blue) -> "\e[34m"; +color_foreground(magenta) -> "\e[35m"; +color_foreground(cyan) -> "\e[36m"; +color_foreground(white) -> "\e[37m". + +color_bold() -> "\e[1m". +reset_color() -> "\e[0m".