web/lib/Zend/Log/Formatter/Xml.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Log
       
    17  * @subpackage Formatter
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Xml.php 20104 2010-01-06 21:26:01Z matthew $
       
    21  */
       
    22 
       
    23 /** Zend_Log_Formatter_Interface */
       
    24 require_once 'Zend/Log/Formatter/Interface.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Log
       
    29  * @subpackage Formatter
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  * @version    $Id: Xml.php 20104 2010-01-06 21:26:01Z matthew $
       
    33  */
       
    34 class Zend_Log_Formatter_Xml implements Zend_Log_Formatter_Interface
       
    35 {
       
    36     /**
       
    37      * @var Relates XML elements to log data field keys.
       
    38      */
       
    39     protected $_rootElement;
       
    40 
       
    41     /**
       
    42      * @var Relates XML elements to log data field keys.
       
    43      */
       
    44     protected $_elementMap;
       
    45 
       
    46     /**
       
    47      * @var string Encoding to use in XML
       
    48      */
       
    49     protected $_encoding;
       
    50 
       
    51     /**
       
    52      * Class constructor
       
    53      *
       
    54      * @param string $rootElement Name of root element
       
    55      * @param array $elementMap
       
    56      * @param string $encoding Encoding to use (defaults to UTF-8)
       
    57      */
       
    58     public function __construct($rootElement = 'logEntry', $elementMap = null, $encoding = 'UTF-8')
       
    59     {
       
    60         $this->_rootElement = $rootElement;
       
    61         $this->_elementMap  = $elementMap;
       
    62         $this->setEncoding($encoding);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Get encoding
       
    67      *
       
    68      * @return string
       
    69      */
       
    70     public function getEncoding()
       
    71     {
       
    72         return $this->_encoding;
       
    73     }
       
    74 
       
    75     /**
       
    76      * Set encoding
       
    77      *
       
    78      * @param  string $value
       
    79      * @return Zend_Log_Formatter_Xml
       
    80      */
       
    81     public function setEncoding($value)
       
    82     {
       
    83         $this->_encoding = (string) $value;
       
    84         return $this;
       
    85     }
       
    86 
       
    87     /**
       
    88      * Formats data into a single line to be written by the writer.
       
    89      *
       
    90      * @param  array    $event    event data
       
    91      * @return string             formatted line to write to the log
       
    92      */
       
    93     public function format($event)
       
    94     {
       
    95         if ($this->_elementMap === null) {
       
    96             $dataToInsert = $event;
       
    97         } else {
       
    98             $dataToInsert = array();
       
    99             foreach ($this->_elementMap as $elementName => $fieldKey) {
       
   100                 $dataToInsert[$elementName] = $event[$fieldKey];
       
   101             }
       
   102         }
       
   103 
       
   104         $enc = $this->getEncoding();
       
   105         $dom = new DOMDocument('1.0', $enc);
       
   106         $elt = $dom->appendChild(new DOMElement($this->_rootElement));
       
   107 
       
   108         foreach ($dataToInsert as $key => $value) {
       
   109             if($key == "message") {
       
   110                 $value = htmlspecialchars($value, ENT_COMPAT, $enc);
       
   111             }
       
   112             $elt->appendChild(new DOMElement($key, $value));
       
   113         }
       
   114 
       
   115         $xml = $dom->saveXML();
       
   116         $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
       
   117 
       
   118         return $xml . PHP_EOL;
       
   119     }
       
   120 
       
   121 }