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