Skip to content

Commit

Permalink
Use correct default colors in ColoredConsoleAppender
Browse files Browse the repository at this point in the history
Fixes #16
  • Loading branch information
thekid committed Nov 5, 2018
1 parent f42e1ef commit 5eef98a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/main/php/util/log/ColoredConsoleAppender.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php namespace util\log\unittest;

use io\streams\MemoryOutputStream;
use io\streams\StringWriter;
use unittest\TestCase;
use util\cmd\Console;
use util\log\ColoredConsoleAppender;
use util\log\Layout;
use util\log\LogCategory;
use util\log\LogLevel;
use util\log\LoggingEvent;

class ColoredConsoleAppenderTest extends TestCase {

/**
* Creates a ColoredConsoleAppender with a given target
*
* @param string|io.streams.OutputStream $target
* @return util.log.LogCategoy
*/
private function category($target) {
return (new LogCategory('default'))->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());
}
}

0 comments on commit 5eef98a

Please sign in to comment.