IntrospectionProcessor.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. * Warning: This only works if the handler processes the logs directly.
  15. * If you put the processor on a handler that is behind a FingersCrossedHandler
  16. * for example, the processor will only be called once the trigger level is reached,
  17. * and all the log records will have the same file/line/.. data from the call that
  18. * triggered the FingersCrossedHandler.
  19. *
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class IntrospectionProcessor
  23. {
  24. /**
  25. * @param array $record
  26. * @return array
  27. */
  28. public function __invoke(array $record)
  29. {
  30. $trace = debug_backtrace();
  31. // skip first since it's always the current method
  32. array_shift($trace);
  33. // the call_user_func call is also skipped
  34. array_shift($trace);
  35. $i = 0;
  36. while (isset($trace[$i]['class']) && false !== strpos($trace[$i]['class'], 'Monolog\\')) {
  37. $i++;
  38. }
  39. // we should have the call source now
  40. $record['extra'] = array_merge(
  41. $record['extra'],
  42. array(
  43. 'file' => isset($trace[$i-1]['file']) ? $trace[$i-1]['file'] : null,
  44. 'line' => isset($trace[$i-1]['line']) ? $trace[$i-1]['line'] : null,
  45. 'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null,
  46. 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
  47. )
  48. );
  49. return $record;
  50. }
  51. }