[18220] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 4 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 5 | * this work for additional information regarding copyright ownership. |
---|
| 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 7 | * (the "License"); you may not use this file except in compliance with |
---|
| 8 | * the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | * |
---|
| 18 | * |
---|
| 19 | * @package log4php |
---|
| 20 | * @subpackage layouts |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * @ignore |
---|
| 25 | */ |
---|
| 26 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..'); |
---|
| 27 | |
---|
| 28 | define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX', 'log4j'); |
---|
| 29 | define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS', 'http://jakarta.apache.org/log4j/'); |
---|
| 30 | |
---|
| 31 | define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX', 'log4php'); |
---|
| 32 | define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS', 'http://logging.apache.org/log4php/'); |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | */ |
---|
| 36 | require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php'); |
---|
| 37 | require_once(LOG4PHP_DIR . '/helpers/LoggerTransform.php'); |
---|
| 38 | require_once(LOG4PHP_DIR . '/LoggerLayout.php'); |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * The output of the LoggerXmlLayout consists of a series of log4php:event elements. |
---|
| 42 | * |
---|
| 43 | * <p>Parameters: {@link $locationInfo}.</p> |
---|
| 44 | * |
---|
| 45 | * <p>It does not output a complete well-formed XML file. |
---|
| 46 | * The output is designed to be included as an external entity in a separate file to form |
---|
| 47 | * a correct XML file.</p> |
---|
| 48 | * |
---|
| 49 | * @author Marco Vassura |
---|
| 50 | * @version $Revision: 635069 $ |
---|
| 51 | * @package log4php |
---|
| 52 | * @subpackage layouts |
---|
| 53 | */ |
---|
| 54 | class LoggerXmlLayout extends LoggerLayout { |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * The <b>LocationInfo</b> option takes a boolean value. By default, |
---|
| 58 | * it is set to false which means there will be no location |
---|
| 59 | * information output by this layout. If the the option is set to |
---|
| 60 | * true, then the file name and line number of the statement at the |
---|
| 61 | * origin of the log statement will be output. |
---|
| 62 | * @var boolean |
---|
| 63 | */ |
---|
| 64 | var $locationInfo = true; |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * @var boolean set the elements namespace |
---|
| 68 | */ |
---|
| 69 | var $log4jNamespace = false; |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * @var string namespace |
---|
| 74 | * @private |
---|
| 75 | */ |
---|
| 76 | var $_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS; |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * @var string namespace prefix |
---|
| 80 | * @private |
---|
| 81 | */ |
---|
| 82 | var $_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX; |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * No options to activate. |
---|
| 86 | */ |
---|
| 87 | function activateOptions() |
---|
| 88 | { |
---|
| 89 | if ($this->getLog4jNamespace()) { |
---|
| 90 | $this->_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS; |
---|
| 91 | $this->_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX; |
---|
| 92 | } else { |
---|
| 93 | $this->_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS; |
---|
| 94 | $this->_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * @return string |
---|
| 100 | */ |
---|
| 101 | function getHeader() |
---|
| 102 | { |
---|
| 103 | return "<{$this->_namespacePrefix}:eventSet ". |
---|
| 104 | "xmlns:{$this->_namespacePrefix}=\"{$this->_namespace}\" ". |
---|
| 105 | "version=\"0.3\" ". |
---|
| 106 | "includesLocationInfo=\"".($this->getLocationInfo() ? "true" : "false")."\"". |
---|
| 107 | ">\r\n"; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * Formats a {@link LoggerLoggingEvent} in conformance with the log4php.dtd. |
---|
| 112 | * |
---|
| 113 | * @param LoggerLoggingEvent $event |
---|
| 114 | * @return string |
---|
| 115 | */ |
---|
| 116 | function format($event) |
---|
| 117 | { |
---|
| 118 | $loggerName = $event->getLoggerName(); |
---|
| 119 | $timeStamp = number_format((float)($event->getTimeStamp() * 1000), 0, '', ''); |
---|
| 120 | $thread = $event->getThreadName(); |
---|
| 121 | $level = $event->getLevel(); |
---|
| 122 | $levelStr = $level->toString(); |
---|
| 123 | |
---|
| 124 | $buf = "<{$this->_namespacePrefix}:event logger=\"{$loggerName}\" level=\"{$levelStr}\" thread=\"{$thread}\" timestamp=\"{$timeStamp}\">\r\n"; |
---|
| 125 | $buf .= "<{$this->_namespacePrefix}:message><![CDATA["; |
---|
| 126 | LoggerTransform::appendEscapingCDATA($buf, $event->getRenderedMessage()); |
---|
| 127 | $buf .= "]]></{$this->_namespacePrefix}:message>\r\n"; |
---|
| 128 | |
---|
| 129 | $ndc = $event->getNDC(); |
---|
| 130 | if($ndc != null) { |
---|
| 131 | $buf .= "<{$this->_namespacePrefix}:NDC><![CDATA["; |
---|
| 132 | LoggerTransform::appendEscapingCDATA($buf, $ndc); |
---|
| 133 | $buf .= "]]></{$this->_namespacePrefix}:NDC>\r\n"; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if ($this->getLocationInfo()) { |
---|
| 137 | $locationInfo = $event->getLocationInformation(); |
---|
| 138 | $buf .= "<{$this->_namespacePrefix}:locationInfo ". |
---|
| 139 | "class=\"" . $locationInfo->getClassName() . "\" ". |
---|
| 140 | "file=\"" . htmlentities($locationInfo->getFileName(), ENT_QUOTES) . "\" ". |
---|
| 141 | "line=\"" . $locationInfo->getLineNumber() . "\" ". |
---|
| 142 | "method=\"" . $locationInfo->getMethodName() . "\" "; |
---|
| 143 | $buf .= "/>\r\n"; |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | $buf .= "</{$this->_namespacePrefix}:event>\r\n\r\n"; |
---|
| 148 | |
---|
| 149 | return $buf; |
---|
| 150 | |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * @return string |
---|
| 155 | */ |
---|
| 156 | function getFooter() |
---|
| 157 | { |
---|
| 158 | |
---|
| 159 | return "</{$this->_namespacePrefix}:eventSet>\r\n"; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * @return boolean |
---|
| 164 | */ |
---|
| 165 | function getLocationInfo() |
---|
| 166 | { |
---|
| 167 | return $this->locationInfo; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * @return boolean |
---|
| 172 | */ |
---|
| 173 | function getLog4jNamespace() |
---|
| 174 | { |
---|
| 175 | return $this->log4jNamespace; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * The XMLLayout prints and does not ignore exceptions. Hence the |
---|
| 180 | * return value <b>false</b>. |
---|
| 181 | * @return boolean |
---|
| 182 | */ |
---|
| 183 | function ignoresThrowable() |
---|
| 184 | { |
---|
| 185 | return false; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * The {@link $locationInfo} option takes a boolean value. By default, |
---|
| 190 | * it is set to false which means there will be no location |
---|
| 191 | * information output by this layout. If the the option is set to |
---|
| 192 | * true, then the file name and line number of the statement at the |
---|
| 193 | * origin of the log statement will be output. |
---|
| 194 | */ |
---|
| 195 | function setLocationInfo($flag) |
---|
| 196 | { |
---|
| 197 | $this->locationInfo = LoggerOptionConverter::toBoolean($flag, true); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * @param boolean |
---|
| 202 | */ |
---|
| 203 | function setLog4jNamespace($flag) |
---|
| 204 | { |
---|
| 205 | $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, true); |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|