From 5eef98a08e337e19727f4b87a720dfaec6ca7b95 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 5 Nov 2018 21:06:54 +0100 Subject: [PATCH] Use correct default colors in ColoredConsoleAppender Fixes #16 --- ChangeLog.md | 4 + .../util/log/ColoredConsoleAppender.class.php | 6 +- .../ColoredConsoleAppenderTest.class.php | 85 +++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100755 src/test/php/util/log/unittest/ColoredConsoleAppenderTest.class.php diff --git a/ChangeLog.md b/ChangeLog.md index 1f20594..5b8e544 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,10 @@ s Logging change log ## ?.?.? / ????-??-?? +## 9.1.1 / 2018-11-05 + +* Fixed issue #16: Colors incorrect - @thekid + ## 9.1.0 / 2018-09-19 * Merged PR #14: Make layout configurable via "layout" in log configuration diff --git a/src/main/php/util/log/ColoredConsoleAppender.class.php b/src/main/php/util/log/ColoredConsoleAppender.class.php index 6a36a95..25d2bfc 100755 --- a/src/main/php/util/log/ColoredConsoleAppender.class.php +++ b/src/main/php/util/log/ColoredConsoleAppender.class.php @@ -9,6 +9,7 @@ * @see http://www.catalyst.com/support/help/cstools3/visual/terminal/escapeseq.html * @see http://www.termsys.demon.co.uk/vtansi.htm#colors * @see xp://util.log.ConsoleAppender + * @test xp://util.log.unittest.ColoredConsoleAppenderTest */ class ColoredConsoleAppender extends ConsoleAppender { private static $DEFAULTS; @@ -32,8 +33,11 @@ static function __static() { */ public function __construct($target= 'out', $colors= []) { parent::__construct($target); - $this->colors= array_merge(self::$DEFAULTS, $colors); + $this->colors= array_replace(self::$DEFAULTS, $colors); } + + /** @return [:string] */ + public function colors() { return $this->colors; } /** * Append data diff --git a/src/test/php/util/log/unittest/ColoredConsoleAppenderTest.class.php b/src/test/php/util/log/unittest/ColoredConsoleAppenderTest.class.php new file mode 100755 index 0000000..c7c1119 --- /dev/null +++ b/src/test/php/util/log/unittest/ColoredConsoleAppenderTest.class.php @@ -0,0 +1,85 @@ +withAppender( + (new ColoredConsoleAppender(new StringWriter($target)))->withLayout(newinstance(Layout::class, [], [ + 'format' => function(LoggingEvent $event) { + return '[LOG] '.implode(' ', $event->getArguments()); + } + ])) + ); + } + + #[@test] + public function can_create() { + new ColoredConsoleAppender(); + } + + #[@test] + public function writes_to_stdout_by_default() { + $this->assertEquals(Console::$out, (new ColoredConsoleAppender())->writer()); + } + + #[@test] + public function can_overwrite_colors() { + $this->assertEquals('00;30', (new ColoredConsoleAppender('out', [LogLevel::INFO => '00;30']))->colors()[LogLevel::INFO]); + } + + #[@test, @values(map = [ + # 'out' => Console::$out, + # 'err' => Console::$err, + #])] + public function writes_to($param, $writer) { + $this->assertEquals($writer, (new ColoredConsoleAppender($param))->writer()); + } + + #[@test] + public function info() { + $out= new MemoryOutputStream(); + $this->category($out)->info('Test'); + + $this->assertEquals("\033[00;00m[LOG] Test\033[0m", $out->getBytes()); + } + + #[@test] + public function warn() { + $out= new MemoryOutputStream(); + $this->category($out)->warn('Test'); + + $this->assertEquals("\033[00;31m[LOG] Test\033[0m", $out->getBytes()); + } + + #[@test] + public function error() { + $out= new MemoryOutputStream(); + $this->category($out)->error('Test'); + + $this->assertEquals("\033[01;31m[LOG] Test\033[0m", $out->getBytes()); + } + + #[@test] + public function debug() { + $out= new MemoryOutputStream(); + $this->category($out)->debug('Test'); + + $this->assertEquals("\033[00;34m[LOG] Test\033[0m", $out->getBytes()); + } +}