Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
[zendframework/zendframework#2029] Add ability to specify log separat…
Browse files Browse the repository at this point in the history
…or in stream writer

- Default to PHP_EOL
- Allow specifying separator via setter, constructor argument, or
  constructor options
- Separator is appended to line received from formatter
  • Loading branch information
weierophinney committed Jul 31, 2012
1 parent 835c18a commit 309136e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/Writer/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
*/
class Stream extends AbstractWriter
{
/**
* Separator between log entries
*
* @var string
*/
protected $logSeparator = PHP_EOL;

/**
* Holds the PHP stream to log to.
*
Expand All @@ -34,19 +41,21 @@ class Stream extends AbstractWriter
*
* @param string|resource|array|Traversable $streamOrUrl Stream or URL to open as a stream
* @param string|null $mode Mode, only applicable if a URL is given
* @param null|string $logSeparator Log separator string
* @return Stream
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function __construct($streamOrUrl, $mode = null)
public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
{
if ($streamOrUrl instanceof Traversable) {
$streamOrUrl = iterator_to_array($streamOrUrl);
}

if (is_array($streamOrUrl)) {
$mode = isset($streamOrUrl['mode']) ? $streamOrUrl['mode'] : null;
$streamOrUrl = isset($streamOrUrl['stream']) ? $streamOrUrl['stream'] : null;
$mode = isset($streamOrUrl['mode']) ? $streamOrUrl['mode'] : null;
$logSeparator = isset($streamOrUrl['log_separator']) ? $streamOrUrl['log_separator'] : null;
$streamOrUrl = isset($streamOrUrl['stream']) ? $streamOrUrl['stream'] : null;
}

// Setting the default mode
Expand Down Expand Up @@ -80,6 +89,10 @@ public function __construct($streamOrUrl, $mode = null)
}
}

if (null !== $logSeparator) {
$this->setLogSeparator($logSeparator);
}

$this->formatter = new SimpleFormatter();
}

Expand All @@ -92,7 +105,7 @@ public function __construct($streamOrUrl, $mode = null)
*/
protected function doWrite(array $event)
{
$line = $this->formatter->format($event) . PHP_EOL;
$line = $this->formatter->format($event) . $this->logSeparator;

ErrorHandler::start(E_WARNING);
$result = fwrite($this->stream, $line);
Expand All @@ -102,6 +115,28 @@ protected function doWrite(array $event)
}
}

/**
* Set log separator string
*
* @param string $logSeparator
* @return Stream
*/
public function setLogSeparator($logSeparator)
{
$this->logSeparator = (string) $logSeparator;
return $this;
}

/**
* Get log separator string
*
* @return string
*/
public function getLogSeparator()
{
return $this->logSeparator;
}

/**
* Close the stream resource.
*
Expand Down
36 changes: 36 additions & 0 deletions test/Writer/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,40 @@ public function testSettingNewFormatter()

$this->assertContains($expected, $contents);
}

public function testAllowSpecifyingLogSeparator()
{
$stream = fopen('php://memory', 'w+');
$writer = new StreamWriter($stream);
$writer->setLogSeparator('::');

$fields = array('message' => 'message1');
$writer->write($fields);
$fields['message'] = 'message2';
$writer->write($fields);

rewind($stream);
$contents = stream_get_contents($stream);
fclose($stream);

$this->assertRegexp('/message1.*?::.*?message2/', $contents);
$this->assertNotContains(PHP_EOL, $contents);
}

public function testAllowsSpecifyingLogSeparatorAsConstructorArgument()
{
$writer = new StreamWriter('php://memory', 'w+', '::');
$this->assertEquals('::', $writer->getLogSeparator());
}

public function testAllowsSpecifyingLogSeparatorWithinArrayPassedToConstructor()
{
$options = array(
'stream' => 'php://memory',
'mode' => 'w+',
'log_separator' => '::',
);
$writer = new StreamWriter($options);
$this->assertEquals('::', $writer->getLogSeparator());
}
}

0 comments on commit 309136e

Please sign in to comment.