Skip to content

Commit

Permalink
Added flag for PHP_EOL at the end of each message, refs #2
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Jan 11, 2018
1 parent 42143b6 commit 311ae61
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ When desired you can hide the log level from output by creating a logger that do
$logger = StdioLogger::create($loop)->withHideLevel(true);
```

Another option is to write a new line (`PHP_EOL`) after each line, it is off by default
but can be enabled with:

```php
$logger = StdioLogger::create($loop)->withNewLine(true);
```

## Contributing ##

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
18 changes: 18 additions & 0 deletions src/StdioLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

final class StdioLogger extends AbstractLogger
{
const NEW_LINE = PHP_EOL;

/**
* @var Stdio
*/
Expand All @@ -22,6 +24,11 @@ final class StdioLogger extends AbstractLogger
*/
private $hideLevel = false;

/**
* @var bool
*/
private $newLine = false;

/**
* @param WritableStreamInterface $stream
*
Expand All @@ -48,6 +55,14 @@ public function withHideLevel(bool $hideLevel): StdioLogger
return $clone;
}

public function withNewLine(bool $newLine): StdioLogger
{
$clone = clone $this;
$clone->newLine = $newLine;

return $clone;
}

public function log($level, $message, array $context = [])
{
checkCorrectLogLevel($level);
Expand All @@ -56,6 +71,9 @@ public function log($level, $message, array $context = [])
if ($this->hideLevel === false) {
$message = $level . ' ' . $message;
}
if ($this->newLine === true) {
$message .= self::NEW_LINE;
}
$this->stdio->write($message);
}
}
22 changes: 22 additions & 0 deletions tests/StdioLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ public function testWriteHideLevel()
(new StdioLogger($stdio->reveal()))->withHideLevel(true)->log($level, $message);
}

public function testWriteNewLine()
{
$level = LogLevel::INFO;
$message = 'adasads';

$stdio = $this->prophesize(WritableStreamInterface::class);
$stdio->write($level . ' ' . $message . StdioLogger::NEW_LINE)->shouldBeCalled();

(new StdioLogger($stdio->reveal()))->withNewLine(true)->log($level, $message);
}

public function testWriteNewLineHideLevel()
{
$level = LogLevel::INFO;
$message = 'adasads';

$stdio = $this->prophesize(WritableStreamInterface::class);
$stdio->write($message . StdioLogger::NEW_LINE)->shouldBeCalled();

(new StdioLogger($stdio->reveal()))->withHideLevel(true)->withNewLine(true)->log($level, $message);
}

public function testWriteMultiline()
{
$level = LogLevel::INFO;
Expand Down

0 comments on commit 311ae61

Please sign in to comment.