IntrospectionProcessor.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  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. /**
  12. * Injects line/file:class/function where the log message came from
  13. *
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class IntrospectionProcessor
  17. {
  18. /**
  19. * @param array $record
  20. * @return array
  21. */
  22. public function __invoke(array $record)
  23. {
  24. $trace = debug_backtrace();
  25. // skip first since it's always the current method
  26. array_shift($trace);
  27. // the call_user_func call is also skipped
  28. array_shift($trace);
  29. $i = 0;
  30. while (isset($trace[$i]['class']) && false !== strpos($trace[$i]['class'], 'Monolog\\')) {
  31. $i++;
  32. }
  33. // we should have the call source now
  34. $record['extra'] = array_merge(
  35. $record['extra'],
  36. array(
  37. 'file' => isset($trace[$i-1]['file']) ? $trace[$i-1]['file'] : null,
  38. 'line' => isset($trace[$i-1]['line']) ? $trace[$i-1]['line'] : null,
  39. 'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null,
  40. 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
  41. )
  42. );
  43. return $record;
  44. }
  45. }