3 * log4php is a PHP port of the log4j java logging package.
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>
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>
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>
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
25 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX', 'log4j');
26 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS', 'http://jakarta.apache.org/log4j/');
28 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX', 'log4php');
29 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS', 'http://www.vxr.it/log4php/');
33 require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
34 require_once(LOG4PHP_DIR . '/helpers/LoggerTransform.php');
35 require_once(LOG4PHP_DIR . '/LoggerLayout.php');
38 * The output of the LoggerXmlLayout consists of a series of log4php:event elements.
40 * <p>Parameters: {@link $locationInfo}.</p>
42 * <p>It does not output a complete well-formed XML file.
43 * The output is designed to be included as an external entity in a separate file to form
44 * a correct XML file.</p>
46 * @author VxR <vxr@vxr.it>
47 * @version $Revision: 2 $
51 class LoggerXmlLayout extends LoggerLayout {
54 * The <b>LocationInfo</b> option takes a boolean value. By default,
55 * it is set to false which means there will be no location
56 * information output by this layout. If the the option is set to
57 * true, then the file name and line number of the statement at the
58 * origin of the log statement will be output.
61 var $locationInfo = true;
64 * @var boolean set the elements namespace
66 var $log4jNamespace = false;
70 * @var string namespace
73 var $_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS;
76 * @var string namespace prefix
79 var $_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX;
82 * No options to activate.
84 function activateOptions()
86 if ($this->getLog4jNamespace()) {
87 $this->_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS;
88 $this->_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX;
90 $this->_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS;
91 $this->_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX;
100 return "<{$this->_namespacePrefix}:eventSet ".
101 "xmlns:{$this->_namespacePrefix}=\"{$this->_namespace}\" ".
103 "includesLocationInfo=\"".($this->getLocationInfo() ? "true" : "false")."\"".
108 * Formats a {@link LoggerLoggingEvent} in conformance with the log4php.dtd.
110 * @param LoggerLoggingEvent $event
113 function format($event)
115 $loggerName = $event->getLoggerName();
116 $timeStamp = number_format((float)($event->getTimeStamp() * 1000), 0, '', '');
117 $thread = $event->getThreadName();
118 $level = $event->getLevel();
119 $levelStr = $level->toString();
121 $buf = "<{$this->_namespacePrefix}:event logger=\"{$loggerName}\" level=\"{$levelStr}\" thread=\"{$thread}\" timestamp=\"{$timeStamp}\">\r\n";
122 $buf .= "<{$this->_namespacePrefix}:message><![CDATA[";
123 LoggerTransform::appendEscapingCDATA($buf, $event->getRenderedMessage());
124 $buf .= "]]></{$this->_namespacePrefix}:message>\r\n";
126 $ndc = $event->getNDC();
128 $buf .= "<{$this->_namespacePrefix}:NDC><![CDATA[";
129 LoggerTransform::appendEscapingCDATA($buf, $ndc);
130 $buf .= "]]></{$this->_namespacePrefix}:NDC>\r\n";
133 if ($this->getLocationInfo()) {
134 $locationInfo = $event->getLocationInformation();
135 $buf .= "<{$this->_namespacePrefix}:locationInfo ".
136 "class=\"" . $locationInfo->getClassName() . "\" ".
137 "file=\"" . htmlentities($locationInfo->getFileName(), ENT_QUOTES) . "\" ".
138 "line=\"" . $locationInfo->getLineNumber() . "\" ".
139 "method=\"" . $locationInfo->getMethodName() . "\" ";
144 $buf .= "</{$this->_namespacePrefix}:event>\r\n\r\n";
156 return "</{$this->_namespacePrefix}:eventSet>\r\n";
162 function getLocationInfo()
164 return $this->locationInfo;
170 function getLog4jNamespace()
172 return $this->log4jNamespace;
176 * The XMLLayout prints and does not ignore exceptions. Hence the
177 * return value <b>false</b>.
180 function ignoresThrowable()
186 * The {@link $locationInfo} option takes a boolean value. By default,
187 * it is set to false which means there will be no location
188 * information output by this layout. If the the option is set to
189 * true, then the file name and line number of the statement at the
190 * origin of the log statement will be output.
192 function setLocationInfo($flag)
194 $this->locationInfo = LoggerOptionConverter::toBoolean($flag, true);
200 function setLog4jNamespace($flag)
202 $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, true);