| 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 |
/** |
| 13 |
* Injects line/file:class/function where the log message came from |
| 14 |
* |
| 15 |
* Warning: This only works if the handler processes the logs directly. |
| 16 |
* If you put the processor on a handler that is behind a FingersCrossedHandler |
| 17 |
* for example, the processor will only be called once the trigger level is reached, |
| 18 |
* and all the log records will have the same file/line/.. data from the call that |
| 19 |
* triggered the FingersCrossedHandler. |
| 20 |
* |
| 21 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
| 22 |
*/ |
| 23 |
class Monolog_Processor_IntrospectionProcessor implements Monolog_Processor_ProcessorInterface |
| 24 |
{ |
| 25 |
private $level; |
| 26 |
|
| 27 |
private $skipClassesPartials; |
| 28 |
|
| 29 |
public function __construct($level = Monolog_Logger::DEBUG, array $skipClassesPartials = array('Monolog_')) |
| 30 |
{ |
| 31 |
$this->level = $level; |
| 32 |
$this->skipClassesPartials = $skipClassesPartials; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param array $record |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
public function callback(array $record) |
| 41 |
{ |
| 42 |
// return if the level is not high enough |
| 43 |
if ($record['level'] < $this->level) { |
| 44 |
return $record; |
| 45 |
} |
| 46 |
|
| 47 |
$trace = debug_backtrace(); |
| 48 |
|
| 49 |
// skip first since it's always the current method |
| 50 |
array_shift($trace); |
| 51 |
// the call_user_func call is also skipped |
| 52 |
array_shift($trace); |
| 53 |
|
| 54 |
$i = 0; |
| 55 |
|
| 56 |
while (isset($trace[$i]['class'])) { |
| 57 |
foreach ($this->skipClassesPartials as $part) { |
| 58 |
if (strpos($trace[$i]['class'], $part) !== false) { |
| 59 |
$i++; |
| 60 |
continue 2; |
| 61 |
} |
| 62 |
} |
| 63 |
break; |
| 64 |
} |
| 65 |
|
| 66 |
// we should have the call source now |
| 67 |
$record['extra'] = array_merge( |
| 68 |
$record['extra'], |
| 69 |
array( |
| 70 |
'file' => isset($trace[$i - 1]['file']) ? $trace[$i - 1]['file'] : null, |
| 71 |
'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null, |
| 72 |
'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null, |
| 73 |
'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null, |
| 74 |
) |
| 75 |
); |
| 76 |
|
| 77 |
return $record; |
| 78 |
} |
| 79 |
} |
| 80 |
|