Skip to content

Commit 311ae61

Browse files
committed
Added flag for PHP_EOL at the end of each message, refs #2
1 parent 42143b6 commit 311ae61

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ When desired you can hide the log level from output by creating a logger that do
2929
$logger = StdioLogger::create($loop)->withHideLevel(true);
3030
```
3131

32+
Another option is to write a new line (`PHP_EOL`) after each line, it is off by default
33+
but can be enabled with:
34+
35+
```php
36+
$logger = StdioLogger::create($loop)->withNewLine(true);
37+
```
38+
3239
## Contributing ##
3340

3441
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

src/StdioLogger.php

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
final class StdioLogger extends AbstractLogger
1414
{
15+
const NEW_LINE = PHP_EOL;
16+
1517
/**
1618
* @var Stdio
1719
*/
@@ -22,6 +24,11 @@ final class StdioLogger extends AbstractLogger
2224
*/
2325
private $hideLevel = false;
2426

27+
/**
28+
* @var bool
29+
*/
30+
private $newLine = false;
31+
2532
/**
2633
* @param WritableStreamInterface $stream
2734
*
@@ -48,6 +55,14 @@ public function withHideLevel(bool $hideLevel): StdioLogger
4855
return $clone;
4956
}
5057

58+
public function withNewLine(bool $newLine): StdioLogger
59+
{
60+
$clone = clone $this;
61+
$clone->newLine = $newLine;
62+
63+
return $clone;
64+
}
65+
5166
public function log($level, $message, array $context = [])
5267
{
5368
checkCorrectLogLevel($level);
@@ -56,6 +71,9 @@ public function log($level, $message, array $context = [])
5671
if ($this->hideLevel === false) {
5772
$message = $level . ' ' . $message;
5873
}
74+
if ($this->newLine === true) {
75+
$message .= self::NEW_LINE;
76+
}
5977
$this->stdio->write($message);
6078
}
6179
}

tests/StdioLoggerTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ public function testWriteHideLevel()
5656
(new StdioLogger($stdio->reveal()))->withHideLevel(true)->log($level, $message);
5757
}
5858

59+
public function testWriteNewLine()
60+
{
61+
$level = LogLevel::INFO;
62+
$message = 'adasads';
63+
64+
$stdio = $this->prophesize(WritableStreamInterface::class);
65+
$stdio->write($level . ' ' . $message . StdioLogger::NEW_LINE)->shouldBeCalled();
66+
67+
(new StdioLogger($stdio->reveal()))->withNewLine(true)->log($level, $message);
68+
}
69+
70+
public function testWriteNewLineHideLevel()
71+
{
72+
$level = LogLevel::INFO;
73+
$message = 'adasads';
74+
75+
$stdio = $this->prophesize(WritableStreamInterface::class);
76+
$stdio->write($message . StdioLogger::NEW_LINE)->shouldBeCalled();
77+
78+
(new StdioLogger($stdio->reveal()))->withHideLevel(true)->withNewLine(true)->log($level, $message);
79+
}
80+
5981
public function testWriteMultiline()
6082
{
6183
$level = LogLevel::INFO;

0 commit comments

Comments
 (0)