WebProcessor.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 url/method and remote IP of the current web request in all records
  13. *
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class WebProcessor
  17. {
  18. protected $serverData;
  19. /**
  20. * @param mixed $serverData array or object w/ ArrayAccess that provides access to the $_SERVER data
  21. */
  22. public function __construct($serverData = null)
  23. {
  24. if (null === $serverData) {
  25. $this->serverData =& $_SERVER;
  26. } elseif (is_array($serverData) || $serverData instanceof \ArrayAccess) {
  27. $this->serverData = $serverData;
  28. } else {
  29. throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.');
  30. }
  31. }
  32. /**
  33. * @param array $record
  34. * @return array
  35. */
  36. public function __invoke(array $record)
  37. {
  38. // skip processing if for some reason request data
  39. // is not present (CLI or wonky SAPIs)
  40. if (!isset($this->serverData['REQUEST_URI'])) {
  41. return $record;
  42. }
  43. $record['extra'] = array_merge(
  44. $record['extra'],
  45. array(
  46. 'url' => $this->serverData['REQUEST_URI'],
  47. 'ip' => $this->serverData['REMOTE_ADDR'],
  48. 'http_method' => $this->serverData['REQUEST_METHOD'],
  49. )
  50. );
  51. return $record;
  52. }
  53. }