Selaa lähdekoodia

ZF-11161
Zend_Log
Updated Zend_Log_Formatter_Xml to include only empty or scalar values in output, and cast values string before passing to DOMElement


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24079 44c647ce-9c0f-0410-b52a-842ac1e357ba

adamlundrigan 14 vuotta sitten
vanhempi
commit
5daefb8bf5
2 muutettua tiedostoa jossa 27 lisäystä ja 3 poistoa
  1. 5 3
      library/Zend/Log/Formatter/Xml.php
  2. 22 0
      tests/Zend/Log/Formatter/XmlTest.php

+ 5 - 3
library/Zend/Log/Formatter/Xml.php

@@ -146,10 +146,12 @@ class Zend_Log_Formatter_Xml extends Zend_Log_Formatter_Abstract
         $elt = $dom->appendChild(new DOMElement($this->_rootElement));
 
         foreach ($dataToInsert as $key => $value) {
-            if($key == "message") {
-                $value = htmlspecialchars($value, ENT_COMPAT, $enc);
+            if(empty($value) || is_scalar($value)) {
+                if($key == "message") {
+                    $value = htmlspecialchars($value, ENT_COMPAT, $enc);
+                }
+                $elt->appendChild(new DOMElement($key, (string)$value));
             }
-            $elt->appendChild(new DOMElement($key, $value));
         }
 
         $xml = $dom->saveXML();

+ 22 - 0
tests/Zend/Log/Formatter/XmlTest.php

@@ -137,6 +137,28 @@ class Zend_Log_Formatter_XmlTest extends PHPUnit_Framework_TestCase
         $formatter = Zend_Log_Formatter_Xml::factory($options);
         $this->assertType('Zend_Log_Formatter_Xml', $formatter);
     }
+    
+    /**
+     * @group ZF-11161
+     */
+    public function testNonScalarValuesAreExcludedFromFormattedString()
+    {
+        $options = array(
+            'rootElement' => 'log'
+        );
+        $event = array(
+            'message' => 'tottakai',
+            'priority' => 4,
+            'context' => array('test'=>'one'),
+            'reference' => new Zend_Log_Formatter_Xml()
+        );
+        $expected = '<log><message>tottakai</message><priority>4</priority></log>';
+
+        $formatter = new Zend_Log_Formatter_Xml($options);
+        $output = $formatter->format($event);
+        $this->assertContains($expected, $output);
+    }
+    
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_XmlTest::main') {