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

Commit

Permalink
Merge pull request zendframework/zendframework#5383 in master
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Formatter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use DateTime;
use Traversable;
use Zend\Stdlib\ErrorHandler;

class Base implements FormatterInterface
{
Expand Down Expand Up @@ -84,21 +83,24 @@ protected function normalize($value)
$jsonFlags |= defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 0;
}

ErrorHandler::start();
// Error suppression is used in several of these cases as a fix for each of
// #5383 and #4616. Without it, #4616 fails whenever recursion occurs during
// json_encode() operations; usage of a dedicated error handler callback
// causes #5383 to fail when the Logger is being used as an error handler.
// The only viable solution here is error suppression, ugly as it may be.
if ($value instanceof DateTime) {
$value = $value->format($this->getDateTimeFormat());
} elseif ($value instanceof Traversable) {
$value = json_encode(iterator_to_array($value), $jsonFlags);
$value = @json_encode(iterator_to_array($value), $jsonFlags);
} elseif (is_array($value)) {
$value = json_encode($value, $jsonFlags);
$value = @json_encode($value, $jsonFlags);
} elseif (is_object($value) && !method_exists($value, '__toString')) {
$value = sprintf('object(%s) %s', get_class($value), json_encode($value));
$value = sprintf('object(%s) %s', get_class($value), @json_encode($value));
} elseif (is_resource($value)) {
$value = sprintf('resource(%s)', get_resource_type($value));
} elseif (!is_object($value)) {
$value = gettype($value);
}
ErrorHandler::stop();

return (string) $value;
}
Expand Down
25 changes: 25 additions & 0 deletions test/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Zend\Log\Logger;
use Zend\Log\Processor\Backtrace;
use Zend\Log\Writer\Mock as MockWriter;
use Zend\Log\Writer\Stream as StreamWriter;
use Zend\Log\Filter\Mock as MockFilter;
use Zend\Stdlib\SplPriorityQueue;
use Zend\Validator\Digits as DigitsFilter;
Expand Down Expand Up @@ -385,4 +386,28 @@ public function testLogExtraArrayKeyWithNonArrayValue()
$this->assertInstanceOf('Zend\Log\Logger', $logger->info('Hi', array('extra' => '')));
fclose($stream);
}

/**
* @group 5383
*/
public function testErrorHandlerWithStreamWriter()
{
$options = array('errorhandler' => true);
$logger = new Logger($options);
$stream = fopen('php://memory', 'w+');
$streamWriter = new StreamWriter($stream);

// error handler does not like this feature so turn it off
$streamWriter->setConvertWriteErrorsToExceptions(false);
$logger->addWriter($streamWriter);

// we raise two notices - both should be logged
echo $test;
echo $second;

rewind($stream);
$contents = stream_get_contents($stream);
$this->assertContains('test', $contents);
$this->assertContains('second', $contents);
}
}

0 comments on commit 49bc0b3

Please sign in to comment.