source: branches/comu-ver2/data/module/log4php/php5/log4php/spi/LoggerLoggingEvent.php @ 18220

Revision 18220, 11.2 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

Line 
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 spi
21 */
22
23/**
24 * @ignore
25 */
26if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
27 
28/**
29 */
30require_once(LOG4PHP_DIR . '/spi/LoggerLocationInfo.php');
31require_once(LOG4PHP_DIR . '/LoggerManager.php');
32require_once(LOG4PHP_DIR . '/LoggerMDC.php');
33require_once(LOG4PHP_DIR . '/LoggerNDC.php');
34
35/**
36 * The internal representation of logging event.
37 *
38 * @author  Marco Vassura
39 * @version $Revision: 635069 $
40 * @package log4php
41 * @subpackage spi
42 */
43class LoggerLoggingEvent {
44
45        private static $startTime;
46
47    /**
48    * @var string Fully Qualified Class Name of the calling category class.
49    */
50    var $fqcn;
51   
52    /**
53    * @var Logger reference
54    */
55    var $logger = null;
56   
57    /**
58    * The category (logger) name.
59    * This field will be marked as private in future
60    * releases. Please do not access it directly.
61    * Use the {@link getLoggerName()} method instead.
62    * @deprecated
63    */
64    var $categoryName;
65   
66    /**
67    * Level of logging event.
68    * <p> This field should not be accessed directly. You shoud use the
69    * {@link getLevel()} method instead.
70    *
71    * @deprecated
72    * @var LoggerLevel
73    */
74    protected $level;
75   
76    /**
77     * @var string The nested diagnostic context (NDC) of logging event.
78     */
79    var $ndc;
80   
81    /**
82     * Have we tried to do an NDC lookup? If we did, there is no need
83     * to do it again.  Note that its value is always false when
84     * serialized. Thus, a receiving SocketNode will never use it's own
85     * (incorrect) NDC. See also writeObject method.
86     * @var boolean
87     */
88    var $ndcLookupRequired = true;
89   
90    /**
91     * Have we tried to do an MDC lookup? If we did, there is no need
92     * to do it again.  Note that its value is always false when
93     * serialized. See also the getMDC and getMDCCopy methods.
94     * @var boolean 
95     */
96    var $mdcCopyLookupRequired = true;
97   
98    /**
99     * @var mixed The application supplied message of logging event.
100     */
101    var $message;
102   
103    /**
104     * The application supplied message rendered through the log4php
105     * objet rendering mechanism. At present renderedMessage == message.
106     * @var string
107     */
108    var $renderedMessage = null;
109   
110    /**
111     * The name of thread in which this logging event was generated.
112     * log4php saves here the process id via {@link PHP_MANUAL#getmypid getmypid()}
113     * @var mixed
114     */
115    var $threadName = null;
116   
117    /**
118    * The number of seconds elapsed from 1/1/1970 until logging event
119    * was created plus microseconds if available.
120    * @var float
121    */
122    public $timeStamp;
123   
124    /**
125    * @var LoggerLocationInfo Location information for the caller.
126    */
127    var $locationInfo = null;
128   
129    /**
130    * Instantiate a LoggingEvent from the supplied parameters.
131    *
132    * <p>Except {@link $timeStamp} all the other fields of
133    * LoggerLoggingEvent are filled when actually needed.
134    *
135    * @param string $fqcn name of the caller class.
136    * @param mixed $logger The {@link Logger} category of this event or the logger name.
137    * @param LoggerLevel $priority The level of this event.
138    * @param mixed $message The message of this event.
139    * @param integer $timeStamp the timestamp of this logging event.
140    */
141    public function __construct($fqcn, $logger, $priority, $message, $timeStamp = null)
142    {
143        $this->fqcn = $fqcn;
144        if ($logger instanceof Logger) {
145            $this->logger = $logger;
146            $this->categoryName = $logger->getName();
147        } else {
148            $this->categoryName = strval($logger);
149        }
150        $this->level = $priority;
151        $this->message = $message;
152        if ($timeStamp !== null && is_float($timeStamp)) {
153            $this->timeStamp = $timeStamp;
154        } else {
155            if (function_exists('microtime')) {
156                                // get microtime as float
157                $this->timeStamp = microtime(true);
158            } else {
159                $this->timeStamp = floatval(time());
160            }
161        }
162    }
163
164    /**
165     * Set the location information for this logging event. The collected
166     * information is cached for future use.
167     *
168     * <p>This method uses {@link PHP_MANUAL#debug_backtrace debug_backtrace()} function (if exists)
169     * to collect informations about caller.</p>
170     * <p>It only recognize informations generated by {@link Logger} and its subclasses.</p>
171     * @return LoggerLocationInfo
172     */
173    public function getLocationInformation()
174    {
175        if($this->locationInfo === null) {
176
177            $locationInfo = array();
178
179            if (function_exists('debug_backtrace')) {
180                $trace = debug_backtrace();
181                $prevHop = null;
182                // make a downsearch to identify the caller
183                $hop = array_pop($trace);
184                while ($hop !== null) {
185                    $className = @strtolower($hop['class']);
186                    if ( !empty($className) and ($className == 'logger' or $className == 'loggercategory' or
187                                @strtolower(get_parent_class($className)) == 'logger' or
188                                @strtolower(get_parent_class($className)) == 'loggercategory')) {
189                        $locationInfo['line'] = $hop['line'];
190                        $locationInfo['file'] = $hop['file'];                         
191                        break;
192                    }
193                    $prevHop = $hop;
194                    $hop = array_pop($trace);
195                }
196                $locationInfo['class'] = isset($prevHop['class']) ? $prevHop['class'] : 'main';
197                if (isset($prevHop['function']) and
198                    $prevHop['function'] !== 'include' and
199                    $prevHop['function'] !== 'include_once' and
200                    $prevHop['function'] !== 'require' and
201                    $prevHop['function'] !== 'require_once') {                                       
202   
203                    $locationInfo['function'] = $prevHop['function'];
204                } else {
205                    $locationInfo['function'] = 'main';
206                }
207            }
208                     
209            $this->locationInfo = new LoggerLocationInfo($locationInfo, $this->fqcn);
210        }
211        return $this->locationInfo;
212    }
213
214    /**
215     * Return the level of this event. Use this form instead of directly
216     * accessing the {@link $level} field.
217     * @return LoggerLevel 
218     */
219    public function getLevel()
220    {
221        return $this->level;
222    }
223
224    /**
225     * Return the name of the logger. Use this form instead of directly
226     * accessing the {@link $categoryName} field.
227     * @return string 
228     */
229    public function getLoggerName()
230    {
231        return $this->categoryName;
232    }
233
234    /**
235     * Return the message for this logging event.
236     *
237     * <p>Before serialization, the returned object is the message
238     * passed by the user to generate the logging event. After
239     * serialization, the returned value equals the String form of the
240     * message possibly after object rendering.
241     * @return mixed
242     */
243    public function getMessage()
244    {
245        if($this->message !== null) {
246            return $this->message;
247        } else {
248            return $this->getRenderedMessage();
249        }
250    }
251
252    /**
253     * This method returns the NDC for this event. It will return the
254     * correct content even if the event was generated in a different
255     * thread or even on a different machine. The {@link LoggerNDC::get()} method
256     * should <b>never</b> be called directly.
257     * @return string 
258     */
259    public function getNDC()
260    {
261        if ($this->ndcLookupRequired) {
262            $this->ndcLookupRequired = false;
263            $this->ndc = implode(' ',LoggerNDC::get());
264        }
265        return $this->ndc;
266    }
267
268
269    /**
270     * Returns the the context corresponding to the <code>key</code>
271     * parameter.
272     * @return string
273     */
274    public function getMDC($key)
275    {
276        return LoggerMDC::get($key);
277    }
278
279    /**
280     * Render message.
281     * @return string
282     */
283    public function getRenderedMessage()
284    {
285        if($this->renderedMessage === null and $this->message !== null) {
286            if (is_string($this->message)) {
287                    $this->renderedMessage = $this->message;
288            } else {
289                if ($this->logger !== null) {
290                    $repository = $this->logger->getLoggerRepository();
291                } else {
292                    $repository = LoggerManager::getLoggerRepository();
293                }
294                if (method_exists($repository, 'getRendererMap')) {
295                    $rendererMap = $repository->getRendererMap();
296                        $this->renderedMessage= $rendererMap->findAndRender($this->message);
297                    } else {
298                        $this->renderedMessage = (string)$this->message;
299                    }
300            }
301        }
302        return $this->renderedMessage;
303    }
304
305    /**
306     * Returns the time when the application started, in seconds
307     * elapsed since 01.01.1970 plus microseconds if available.
308     *
309     * @return float
310     * @static
311     */
312    public static function getStartTime() {
313        if (!isset(self::$startTime)) {
314            if (function_exists('microtime')) {
315                // microtime as float
316                self::$startTime = microtime(true);
317            } else {
318                self::$startTime = floatval(time());
319            }
320        }
321        return self::$startTime;
322    }
323   
324    /**
325     * @return float
326     */
327    public function getTimeStamp()
328    {
329        return $this->timeStamp;
330    }
331   
332    /**
333     * @return mixed
334     */
335    public function getThreadName()
336    {
337        if ($this->threadName === null)
338            $this->threadName = (string)getmypid();
339        return $this->threadName;
340    }
341
342    /**
343     * @return mixed null
344     */
345    public function getThrowableInformation()
346    {
347        return null;
348    }
349   
350    /**
351     * Serialize this object
352     * @return string
353     */
354    public function toString()
355    {
356        serialize($this);
357    }
358   
359    /**
360     * Avoid serialization of the {@link $logger} object
361     */
362    public function __sleep()
363    {
364        return array(
365            'fqcn','categoryName',
366            'level',
367            'ndc','ndcLookupRequired',
368            'message','renderedMessage',
369            'threadName',
370            'timeStamp',
371            'locationInfo'
372        );
373    }
374
375}
376
377LoggerLoggingEvent::getStartTime();
378
Note: See TracBrowser for help on using the repository browser.