]> git.mxchange.org Git - friendica.git/blob - src/Util/Logger/FriendicaProcessor.php
Display mentioned users and in the comment box by default
[friendica.git] / src / Util / Logger / FriendicaProcessor.php
1 <?php
2
3 namespace Friendica\Util\Logger;
4
5 use Monolog\Logger;
6 use Monolog\Processor\ProcessorInterface;
7
8 /**
9  * Injects line/file//function where the log message came from
10  *
11  * Based on the class IntrospectionProcessor without the "class" information
12  * @see IntrospectionProcessor
13  */
14 class FriendicaProcessor implements ProcessorInterface
15 {
16         private $level;
17
18         private $skipStackFramesCount;
19
20         private $skipFunctions = [
21                 'call_user_func',
22                 'call_user_func_array',
23         ];
24
25         private $skipFiles = [
26                 'Logger.php'
27         ];
28
29         /**
30          * @param string|int $level The minimum logging level at which this Processor will be triggered
31          * @param int $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
32          */
33         public function __construct($level = Logger::DEBUG, $skipStackFramesCount = 0)
34         {
35                 $this->level = Logger::toMonologLevel($level);
36                 $this->skipStackFramesCount = $skipStackFramesCount;
37         }
38
39         public function __invoke(array $record)
40         {
41                 // return if the level is not high enough
42                 if ($record['level'] < $this->level) {
43                         return $record;
44                 }
45
46                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
47
48                 $i = 1;
49
50                 // Skip everything that we shouldn't display
51                 while (in_array($trace[$i]['function'], $this->skipFunctions) ||
52                         !isset($trace[$i - 1]['file']) ||
53                         in_array(basename($trace[$i - 1]['file']), $this->skipFiles)) {
54                         $i++;
55                 }
56
57                 // we should have the call source now
58                 $record['extra'] = array_merge(
59                         $record['extra'],
60                         [
61                                 'file'      => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
62                                 'line'      => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
63                                 'function'  => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
64                         ]
65                 );
66
67                 return $record;
68         }
69 }