IntrospectionProcessor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Processor;
  11. use Monolog\Logger;
  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 IntrospectionProcessor implements ProcessorInterface
  24. {
  25. /** @var int */
  26. private $level;
  27. /** @var string[] */
  28. private $skipClassesPartials;
  29. /** @var int */
  30. private $skipStackFramesCount;
  31. /** @var string[] */
  32. private $skipFunctions = [
  33. 'call_user_func',
  34. 'call_user_func_array',
  35. ];
  36. /**
  37. * @param string|int $level The minimum logging level at which this Processor will be triggered
  38. * @param string[] $skipClassesPartials
  39. */
  40. public function __construct($level = Logger::DEBUG, array $skipClassesPartials = [], int $skipStackFramesCount = 0)
  41. {
  42. $this->level = Logger::toMonologLevel($level);
  43. $this->skipClassesPartials = array_merge(['Monolog\\'], $skipClassesPartials);
  44. $this->skipStackFramesCount = $skipStackFramesCount;
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function __invoke(array $record): array
  50. {
  51. // return if the level is not high enough
  52. if ($record['level'] < $this->level) {
  53. return $record;
  54. }
  55. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  56. // skip first since it's always the current method
  57. array_shift($trace);
  58. // the call_user_func call is also skipped
  59. array_shift($trace);
  60. $i = 0;
  61. while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
  62. if (isset($trace[$i]['class'])) {
  63. foreach ($this->skipClassesPartials as $part) {
  64. if (strpos($trace[$i]['class'], $part) !== false) {
  65. $i++;
  66. continue 2;
  67. }
  68. }
  69. } elseif (in_array($trace[$i]['function'], $this->skipFunctions)) {
  70. $i++;
  71. continue;
  72. }
  73. break;
  74. }
  75. $i += $this->skipStackFramesCount;
  76. // we should have the call source now
  77. $record['extra'] = array_merge(
  78. $record['extra'],
  79. [
  80. 'file' => isset($trace[$i - 1]['file']) ? $trace[$i - 1]['file'] : null,
  81. 'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
  82. 'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null,
  83. 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
  84. ]
  85. );
  86. return $record;
  87. }
  88. /**
  89. * @param array[] $trace
  90. */
  91. private function isTraceClassOrSkippedFunction(array $trace, int $index): bool
  92. {
  93. if (!isset($trace[$index])) {
  94. return false;
  95. }
  96. return isset($trace[$index]['class']) || in_array($trace[$index]['function'], $this->skipFunctions);
  97. }
  98. }