Skip to content

Commit 5eef98a

Browse files
committed
Use correct default colors in ColoredConsoleAppender
Fixes #16
1 parent f42e1ef commit 5eef98a

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ s Logging change log
33

44
## ?.?.? / ????-??-??
55

6+
## 9.1.1 / 2018-11-05
7+
8+
* Fixed issue #16: Colors incorrect - @thekid
9+
610
## 9.1.0 / 2018-09-19
711

812
* Merged PR #14: Make layout configurable via "layout" in log configuration

src/main/php/util/log/ColoredConsoleAppender.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @see http://www.catalyst.com/support/help/cstools3/visual/terminal/escapeseq.html
1010
* @see http://www.termsys.demon.co.uk/vtansi.htm#colors
1111
* @see xp://util.log.ConsoleAppender
12+
* @test xp://util.log.unittest.ColoredConsoleAppenderTest
1213
*/
1314
class ColoredConsoleAppender extends ConsoleAppender {
1415
private static $DEFAULTS;
@@ -32,8 +33,11 @@ static function __static() {
3233
*/
3334
public function __construct($target= 'out', $colors= []) {
3435
parent::__construct($target);
35-
$this->colors= array_merge(self::$DEFAULTS, $colors);
36+
$this->colors= array_replace(self::$DEFAULTS, $colors);
3637
}
38+
39+
/** @return [:string] */
40+
public function colors() { return $this->colors; }
3741

3842
/**
3943
* Append data
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php namespace util\log\unittest;
2+
3+
use io\streams\MemoryOutputStream;
4+
use io\streams\StringWriter;
5+
use unittest\TestCase;
6+
use util\cmd\Console;
7+
use util\log\ColoredConsoleAppender;
8+
use util\log\Layout;
9+
use util\log\LogCategory;
10+
use util\log\LogLevel;
11+
use util\log\LoggingEvent;
12+
13+
class ColoredConsoleAppenderTest extends TestCase {
14+
15+
/**
16+
* Creates a ColoredConsoleAppender with a given target
17+
*
18+
* @param string|io.streams.OutputStream $target
19+
* @return util.log.LogCategoy
20+
*/
21+
private function category($target) {
22+
return (new LogCategory('default'))->withAppender(
23+
(new ColoredConsoleAppender(new StringWriter($target)))->withLayout(newinstance(Layout::class, [], [
24+
'format' => function(LoggingEvent $event) {
25+
return '[LOG] '.implode(' ', $event->getArguments());
26+
}
27+
]))
28+
);
29+
}
30+
31+
#[@test]
32+
public function can_create() {
33+
new ColoredConsoleAppender();
34+
}
35+
36+
#[@test]
37+
public function writes_to_stdout_by_default() {
38+
$this->assertEquals(Console::$out, (new ColoredConsoleAppender())->writer());
39+
}
40+
41+
#[@test]
42+
public function can_overwrite_colors() {
43+
$this->assertEquals('00;30', (new ColoredConsoleAppender('out', [LogLevel::INFO => '00;30']))->colors()[LogLevel::INFO]);
44+
}
45+
46+
#[@test, @values(map = [
47+
# 'out' => Console::$out,
48+
# 'err' => Console::$err,
49+
#])]
50+
public function writes_to($param, $writer) {
51+
$this->assertEquals($writer, (new ColoredConsoleAppender($param))->writer());
52+
}
53+
54+
#[@test]
55+
public function info() {
56+
$out= new MemoryOutputStream();
57+
$this->category($out)->info('Test');
58+
59+
$this->assertEquals("\033[00;00m[LOG] Test\033[0m", $out->getBytes());
60+
}
61+
62+
#[@test]
63+
public function warn() {
64+
$out= new MemoryOutputStream();
65+
$this->category($out)->warn('Test');
66+
67+
$this->assertEquals("\033[00;31m[LOG] Test\033[0m", $out->getBytes());
68+
}
69+
70+
#[@test]
71+
public function error() {
72+
$out= new MemoryOutputStream();
73+
$this->category($out)->error('Test');
74+
75+
$this->assertEquals("\033[01;31m[LOG] Test\033[0m", $out->getBytes());
76+
}
77+
78+
#[@test]
79+
public function debug() {
80+
$out= new MemoryOutputStream();
81+
$this->category($out)->debug('Test');
82+
83+
$this->assertEquals("\033[00;34m[LOG] Test\033[0m", $out->getBytes());
84+
}
85+
}

0 commit comments

Comments
 (0)