scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Processor;
13  
14 use Monolog\Logger;
15  
16 /**
17 * Injects line/file:class/function where the log message came from
18 *
19 * Warning: This only works if the handler processes the logs directly.
20 * If you put the processor on a handler that is behind a FingersCrossedHandler
21 * for example, the processor will only be called once the trigger level is reached,
22 * and all the log records will have the same file/line/.. data from the call that
23 * triggered the FingersCrossedHandler.
24 *
25 * @author Jordi Boggiano <j.boggiano@seld.be>
26 */
27 class IntrospectionProcessor
28 {
29 private $level;
30  
31 private $skipClassesPartials;
32  
33 private $skipStackFramesCount;
34  
35 private $skipFunctions = array(
36 'call_user_func',
37 'call_user_func_array',
38 );
39  
40 public function __construct($level = Logger::DEBUG, array $skipClassesPartials = array(), $skipStackFramesCount = 0)
41 {
42 $this->level = Logger::toMonologLevel($level);
43 $this->skipClassesPartials = array_merge(array('Monolog\\'), $skipClassesPartials);
44 $this->skipStackFramesCount = $skipStackFramesCount;
45 }
46  
47 /**
48 * @param array $record
49 * @return array
50 */
51 public function __invoke(array $record)
52 {
53 // return if the level is not high enough
54 if ($record['level'] < $this->level) {
55 return $record;
56 }
57  
58 /*
59 * http://php.net/manual/en/function.debug-backtrace.php
60 * As of 5.3.6, DEBUG_BACKTRACE_IGNORE_ARGS option was added.
61 * Any version less than 5.3.6 must use the DEBUG_BACKTRACE_IGNORE_ARGS constant value '2'.
62 */
63 $trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
64  
65 // skip first since it's always the current method
66 array_shift($trace);
67 // the call_user_func call is also skipped
68 array_shift($trace);
69  
70 $i = 0;
71  
72 while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
73 if (isset($trace[$i]['class'])) {
74 foreach ($this->skipClassesPartials as $part) {
75 if (strpos($trace[$i]['class'], $part) !== false) {
76 $i++;
77 continue 2;
78 }
79 }
80 } elseif (in_array($trace[$i]['function'], $this->skipFunctions)) {
81 $i++;
82 continue;
83 }
84  
85 break;
86 }
87  
88 $i += $this->skipStackFramesCount;
89  
90 // we should have the call source now
91 $record['extra'] = array_merge(
92 $record['extra'],
93 array(
94 'file' => isset($trace[$i - 1]['file']) ? $trace[$i - 1]['file'] : null,
95 'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
96 'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null,
97 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
98 )
99 );
100  
101 return $record;
102 }
103  
104 private function isTraceClassOrSkippedFunction(array $trace, $index)
105 {
106 if (!isset($trace[$index])) {
107 return false;
108 }
109  
110 return isset($trace[$index]['class']) || in_array($trace[$index]['function'], $this->skipFunctions);
111 }
112 }