Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ZF2-549 Zend\Log\Formatter\ErrorHandler does not handle complex events g... #2342

Merged
merged 1 commit into from

3 participants

@lowcoders

...enerated by error handlers

See http://framework.zend.com/issues/browse/ZF2-549

Includes tests cases

@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [ZF2-549][#2342] CS fixes
- Trailing whitespace
272c704
@weierophinney weierophinney merged commit 818f671 into from
@weierophinney

Merged to release and master branches.

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [ZF2-549][#2342] CS fixes
- Trailing whitespace
7482985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
39 library/Zend/Log/Formatter/ErrorHandler.php
@@ -35,17 +35,42 @@ public function format($event)
if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) {
$event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat());
}
+
+ foreach ($this->buildReplacementsFromArray($event) as $name => $value) {
+ $output = str_replace("%$name%", $value, $output);
+ }
+
+ return $output;
+ }
- foreach ($event as $name => $value) {
- if (is_array($value)) {
- foreach ($value as $sname => $svalue) {
- $output = str_replace("%{$name}[{$sname}]%", $svalue, $output);
+ /**
+ * Flatten the multi-dimensional $event array into a single dimensional
+ * array
+ *
+ * @param array $event
+ * @param string $key
+ * @return array
+ */
+ protected function buildReplacementsFromArray ($event, $key = null)
+ {
+ $result = array();
+ foreach ($event as $index => $value) {
+ $nextIndex = $key === null ? $index : $key . '[' . $index . ']';
+ if ($value === null) {
+ continue;
+ }
+ if (! is_array($value)) {
+ if ($key === null) {
+ $result[$nextIndex] = $value;
+ } else {
+ if (! is_object($value) || method_exists($value, "__toString")) {
+ $result[$nextIndex] = $value;
+ }
}
} else {
- $output = str_replace("%$name%", $value, $output);
+ $result = array_merge($result, $this->buildReplacementsFromArray($value, $nextIndex));
}
}
-
- return $output;
+ return $result;
}
}
View
36 tests/ZendTest/Log/Formatter/ErrorHandlerTest.php
@@ -12,6 +12,8 @@
use DateTime;
use Zend\Log\Formatter\ErrorHandler;
+use ZendTest\Log\TestAsset\StringObject;
+use ZendTest\Log\TestAsset\NotStringObject;
/**
* @category Zend
@@ -33,7 +35,8 @@ public function testFormat()
'extra' => array (
'errno' => 1,
'file' => 'test.php',
- 'line' => 1
+ 'line' => 1,
+ 'context' => array('object' => new DateTime(), 'string' => 'test')
)
);
$formatter = new ErrorHandler();
@@ -50,4 +53,35 @@ public function testSetDateTimeFormat()
$this->assertSame($formatter, $formatter->setDateTimeFormat('r'));
$this->assertEquals('r', $formatter->getDateTimeFormat());
}
+
+ public function testComplexEvent ()
+ {
+ $date = new DateTime();
+ $stringObject = new StringObject();
+ $event = array(
+ 'timestamp' => $date,
+ 'message' => 'test',
+ 'priority' => 1,
+ 'priorityName' => 'CRIT',
+ 'extra' => array(
+ 'errno' => 1,
+ 'file' => 'test.php',
+ 'line' => 1,
+ 'context' => array(
+ 'object1' => new StringObject(),
+ 'object2' => new NotStringObject(),
+ 'string' => 'test1',
+ 'array' => array(
+ 'key' => 'test2'
+ )
+ )
+ )
+ );
+ $formatString = '%extra[context][object1]% %extra[context][object2]% %extra[context][string]% %extra[context][array]% %extra[context][array][key]%';
+ $formatter = new ErrorHandler($formatString);
+ $output = $formatter->format($event);
+ $this->assertEquals($stringObject->__toString() .' %extra[context][object2]% test1 %extra[context][array]% test2', $output);
+ }
+
+
}
View
16 tests/ZendTest/Log/TestAsset/NotStringObject.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Log
+ */
+
+namespace ZendTest\Log\TestAsset;
+
+class NotStringObject
+{
+ // This object has no __toString method
+}
Something went wrong with that request. Please try again.