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.
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
23 /** Zend_Log_Formatter_Interface */
24 require_once 'Zend/Log/Formatter/Interface.php';
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
34 class Zend_Log_Formatter_Xml
implements Zend_Log_Formatter_Interface
37 * @var Relates XML elements to log data field keys.
39 protected $_rootElement;
42 * @var Relates XML elements to log data field keys.
44 protected $_elementMap;
47 * @var string Encoding to use in XML
54 * @param string $rootElement Name of root element
55 * @param array $elementMap
56 * @param string $encoding Encoding to use (defaults to UTF-8)
58 public function __construct($rootElement = 'logEntry', $elementMap = null, $encoding = 'UTF-8')
60 $this->_rootElement
= $rootElement;
61 $this->_elementMap
= $elementMap;
62 $this->setEncoding($encoding);
70 public function getEncoding()
72 return $this->_encoding
;
78 * @param string $value
79 * @return Zend_Log_Formatter_Xml
81 public function setEncoding($value)
83 $this->_encoding
= (string) $value;
88 * Formats data into a single line to be written by the writer.
90 * @param array $event event data
91 * @return string formatted line to write to the log
93 public function format($event)
95 if ($this->_elementMap
=== null) {
96 $dataToInsert = $event;
98 $dataToInsert = array();
99 foreach ($this->_elementMap
as $elementName => $fieldKey) {
100 $dataToInsert[$elementName] = $event[$fieldKey];
104 $enc = $this->getEncoding();
105 $dom = new DOMDocument('1.0', $enc);
106 $elt = $dom->appendChild(new DOMElement($this->_rootElement
));
108 foreach ($dataToInsert as $key => $value) {
109 if($key == "message") {
110 $value = htmlspecialchars($value, ENT_COMPAT
, $enc);
112 $elt->appendChild(new DOMElement($key, $value));
115 $xml = $dom->saveXML();
116 $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
118 return $xml . PHP_EOL
;