OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / lib / log4php / layouts / LoggerXmlLayout.php
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 /**
21  * @ignore 
22  */
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
24
25 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX',     'log4j');
26 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS',            'http://jakarta.apache.org/log4j/');
27
28 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX',   'log4php');
29 define('LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS',          'http://www.vxr.it/log4php/');
30
31 /**
32  */
33 require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
34 require_once(LOG4PHP_DIR . '/helpers/LoggerTransform.php');
35 require_once(LOG4PHP_DIR . '/LoggerLayout.php');
36
37 /**
38  * The output of the LoggerXmlLayout consists of a series of log4php:event elements. 
39  * 
40  * <p>Parameters: {@link $locationInfo}.</p>
41  *
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>
45  *
46  * @author VxR <vxr@vxr.it>
47  * @version $Revision: 2 $
48  * @package log4php
49  * @subpackage layouts
50  */
51 class LoggerXmlLayout extends LoggerLayout {
52
53     /**
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.
59      * @var boolean
60      */
61     var $locationInfo = true;
62   
63     /**
64      * @var boolean set the elements namespace
65      */
66     var $log4jNamespace = false;
67     
68     
69     /**
70      * @var string namespace
71      * @private
72      */
73     var $_namespace = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS;
74     
75     /**
76      * @var string namespace prefix
77      * @private
78      */
79     var $_namespacePrefix = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX;
80      
81     /** 
82      * No options to activate. 
83      */
84     function activateOptions()
85     {
86         if ($this->getLog4jNamespace()) {
87             $this->_namespace        = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS;
88             $this->_namespacePrefix  = LOG4PHP_LOGGER_XML_LAYOUT_LOG4J_NS_PREFIX;
89         } else {
90             $this->_namespace        = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS;
91             $this->_namespacePrefix  = LOG4PHP_LOGGER_XML_LAYOUT_LOG4PHP_NS_PREFIX;
92         }     
93     }
94     
95     /**
96      * @return string
97      */
98     function getHeader()
99     {
100         return "<{$this->_namespacePrefix}:eventSet ".
101                     "xmlns:{$this->_namespacePrefix}=\"{$this->_namespace}\" ".
102                     "version=\"0.3\" ".
103                     "includesLocationInfo=\"".($this->getLocationInfo() ? "true" : "false")."\"".
104                ">\r\n";
105     }
106
107     /**
108      * Formats a {@link LoggerLoggingEvent} in conformance with the log4php.dtd.
109      *
110      * @param LoggerLoggingEvent $event
111      * @return string
112      */
113     function format($event)
114     {
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();
120
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";        
125
126         $ndc = $event->getNDC();
127         if($ndc != null) {
128             $buf .= "<{$this->_namespacePrefix}:NDC><![CDATA[";
129             LoggerTransform::appendEscapingCDATA($buf, $ndc);
130             $buf .= "]]></{$this->_namespacePrefix}:NDC>\r\n";       
131         }
132
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() . "\" ";
140             $buf .= "/>\r\n";
141
142         }
143
144         $buf .= "</{$this->_namespacePrefix}:event>\r\n\r\n";
145         
146         return $buf;
147
148     }
149     
150     /**
151      * @return string
152      */
153     function getFooter()
154     {
155
156         return "</{$this->_namespacePrefix}:eventSet>\r\n";
157     }
158     
159     /**
160      * @return boolean
161      */
162     function getLocationInfo()
163     {
164         return $this->locationInfo;
165     }
166   
167     /**
168      * @return boolean
169      */
170     function getLog4jNamespace()
171     {
172         return $this->log4jNamespace;
173     }
174
175     /**
176      * The XMLLayout prints and does not ignore exceptions. Hence the
177      * return value <b>false</b>.
178      * @return boolean
179      */
180     function ignoresThrowable()
181     {
182         return false;
183     }
184     
185     /**
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.
191      */
192     function setLocationInfo($flag)
193     {
194         $this->locationInfo = LoggerOptionConverter::toBoolean($flag, true);
195     }
196   
197     /**
198      * @param boolean
199      */
200     function setLog4jNamespace($flag)
201     {
202         $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, true);
203     }
204 }
205
206 ?>