PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/SyracavaPHP/syracava/ext/log4php/layouts/LoggerXmlLayout.php

http://syracava.googlecode.com/
PHP | 206 lines | 87 code | 28 blank | 91 comment | 6 complexity | 268a8280a8d35d7a956b02498c605c4e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage layouts
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX', 'log4j');
  24. define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS', 'http://jakarta.apache.org/log4j/');
  25. define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX', 'log4php');
  26. define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS', 'http://www.vxr.it/log4php/');
  27. /**
  28. */
  29. require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
  30. require_once(LOG4PHP_DIR . '/helpers/LoggerTransform.php');
  31. require_once(LOG4PHP_DIR . '/LoggerLayout.php');
  32. /**
  33. * The output of the LoggerXmlLayout consists of a series of log4php:event elements.
  34. *
  35. * <p>Parameters: {@link $locationInfo}.</p>
  36. *
  37. * <p>It does not output a complete well-formed XML file.
  38. * The output is designed to be included as an external entity in a separate file to form
  39. * a correct XML file.</p>
  40. *
  41. * @author VxR <vxr@vxr.it>
  42. * @version $Revision: 125 $
  43. * @package log4php
  44. * @subpackage layouts
  45. */
  46. class LoggerXmlLayout extends LoggerLayout {
  47. /**
  48. * The <b>LocationInfo</b> option takes a boolean value. By default,
  49. * it is set to false which means there will be no location
  50. * information output by this layout. If the the option is set to
  51. * true, then the file name and line number of the statement at the
  52. * origin of the log statement will be output.
  53. * @var boolean
  54. */
  55. var $locationInfo = true;
  56. /**
  57. * @var boolean set the elements namespace
  58. */
  59. var $log4jNamespace = false;
  60. /**
  61. * @var string namespace
  62. * @private
  63. */
  64. var $_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS;
  65. /**
  66. * @var string namespace prefix
  67. * @private
  68. */
  69. var $_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX;
  70. /**
  71. * No options to activate.
  72. */
  73. function activateOptions()
  74. {
  75. if ($this->getLog4jNamespace()) {
  76. $this->_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS;
  77. $this->_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX;
  78. } else {
  79. $this->_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS;
  80. $this->_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX;
  81. }
  82. }
  83. /**
  84. * @return string
  85. */
  86. function getHeader()
  87. {
  88. return "<{$this->_namespacePrefix}:eventSet ".
  89. "xmlns:{$this->_namespacePrefix}=\"{$this->_namespace}\" ".
  90. "version=\"0.3\" ".
  91. "includesLocationInfo=\"".($this->getLocationInfo() ? "true" : "false")."\"".
  92. ">\r\n";
  93. }
  94. /**
  95. * Formats a {@link LoggerLoggingEvent} in conformance with the log4php.dtd.
  96. *
  97. * @param LoggerLoggingEvent $event
  98. * @return string
  99. */
  100. function format($event)
  101. {
  102. $loggerName = $event->getLoggerName();
  103. $timeStamp = number_format((float)($event->getTimeStamp() * 1000), 0, '', '');
  104. $thread = $event->getThreadName();
  105. $level = $event->getLevel();
  106. $levelStr = $level->toString();
  107. $buf = "<{$this->_namespacePrefix}:event logger=\"{$loggerName}\" level=\"{$levelStr}\" thread=\"{$thread}\" timestamp=\"{$timeStamp}\">\r\n";
  108. $buf .= "<{$this->_namespacePrefix}:message><![CDATA[";
  109. LoggerTransform::appendEscapingCDATA($buf, $event->getRenderedMessage());
  110. $buf .= "]]></{$this->_namespacePrefix}:message>\r\n";
  111. $ndc = $event->getNDC();
  112. if($ndc != null) {
  113. $buf .= "<{$this->_namespacePrefix}:NDC><![CDATA[";
  114. LoggerTransform::appendEscapingCDATA($buf, $ndc);
  115. $buf .= "]]></{$this->_namespacePrefix}:NDC>\r\n";
  116. }
  117. if ($this->getLocationInfo()) {
  118. $locationInfo = $event->getLocationInformation();
  119. $buf .= "<{$this->_namespacePrefix}:locationInfo ".
  120. "class=\"" . $locationInfo->getClassName() . "\" ".
  121. "file=\"" . htmlentities($locationInfo->getFileName(), ENT_QUOTES) . "\" ".
  122. "line=\"" . $locationInfo->getLineNumber() . "\" ".
  123. "method=\"" . $locationInfo->getMethodName() . "\" ";
  124. $buf .= "/>\r\n";
  125. }
  126. $buf .= "</{$this->_namespacePrefix}:event>\r\n\r\n";
  127. return $buf;
  128. }
  129. /**
  130. * @return string
  131. */
  132. function getFooter()
  133. {
  134. return "</{$this->_namespacePrefix}:eventSet>\r\n";
  135. }
  136. /**
  137. * @return boolean
  138. */
  139. function getLocationInfo()
  140. {
  141. return $this->locationInfo;
  142. }
  143. /**
  144. * @return boolean
  145. */
  146. function getLog4jNamespace()
  147. {
  148. return $this->log4jNamespace;
  149. }
  150. /**
  151. * The XMLLayout prints and does not ignore exceptions. Hence the
  152. * return value <b>false</b>.
  153. * @return boolean
  154. */
  155. function ignoresThrowable()
  156. {
  157. return false;
  158. }
  159. /**
  160. * The {@link $locationInfo} option takes a boolean value. By default,
  161. * it is set to false which means there will be no location
  162. * information output by this layout. If the the option is set to
  163. * true, then the file name and line number of the statement at the
  164. * origin of the log statement will be output.
  165. */
  166. function setLocationInfo($flag)
  167. {
  168. $this->locationInfo = LoggerOptionConverter::toBoolean($flag, true);
  169. }
  170. /**
  171. * @param boolean
  172. */
  173. function setLog4jNamespace($flag)
  174. {
  175. $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, true);
  176. }
  177. }
  178. ?>