Xml.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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$
  21. */
  22. /** Zend_Log_Formatter_Interface */
  23. require_once 'Zend/Log/Formatter/Interface.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Formatter
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id$
  31. */
  32. class Zend_Log_Formatter_Xml implements Zend_Log_Formatter_Interface
  33. {
  34. /**
  35. * @var Relates XML elements to log data field keys.
  36. */
  37. protected $_rootElement;
  38. /**
  39. * @var Relates XML elements to log data field keys.
  40. */
  41. protected $_elementMap;
  42. /**
  43. * @var string Encoding to use in XML
  44. */
  45. protected $_encoding;
  46. /**
  47. * Class constructor
  48. *
  49. * @param string $rootElement Name of root element
  50. * @param array $elementMap
  51. * @param string $encoding Encoding to use (defaults to UTF-8)
  52. */
  53. public function __construct($rootElement = 'logEntry', $elementMap = null, $encoding = 'UTF-8')
  54. {
  55. $this->_rootElement = $rootElement;
  56. $this->_elementMap = $elementMap;
  57. $this->setEncoding($encoding);
  58. }
  59. /**
  60. * Get encoding
  61. *
  62. * @return string
  63. */
  64. public function getEncoding()
  65. {
  66. return $this->_encoding;
  67. }
  68. /**
  69. * Set encoding
  70. *
  71. * @param string $value
  72. * @return Zend_Log_Formatter_Xml
  73. */
  74. public function setEncoding($value)
  75. {
  76. $this->_encoding = (string) $value;
  77. return $this;
  78. }
  79. /**
  80. * Formats data into a single line to be written by the writer.
  81. *
  82. * @param array $event event data
  83. * @return string formatted line to write to the log
  84. */
  85. public function format($event)
  86. {
  87. if ($this->_elementMap === null) {
  88. $dataToInsert = $event;
  89. } else {
  90. $dataToInsert = array();
  91. foreach ($this->_elementMap as $elementName => $fieldKey) {
  92. $dataToInsert[$elementName] = $event[$fieldKey];
  93. }
  94. }
  95. $enc = $this->getEncoding();
  96. $dom = new DOMDocument('1.0', $enc);
  97. $elt = $dom->appendChild(new DOMElement($this->_rootElement));
  98. foreach ($dataToInsert as $key => $value) {
  99. if($key == "message") {
  100. $value = htmlspecialchars($value, ENT_COMPAT, $enc);
  101. }
  102. $elt->appendChild(new DOMElement($key, $value));
  103. }
  104. $xml = $dom->saveXML();
  105. $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
  106. return $xml . PHP_EOL;
  107. }
  108. }