WebProcessor.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ArrayAccess;
  12. use Monolog\LogRecord;
  13. /**
  14. * Injects url/method and remote IP of the current web request in all records
  15. *
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class WebProcessor implements ProcessorInterface
  19. {
  20. /**
  21. * @var array<string, mixed>|ArrayAccess<string, mixed>
  22. */
  23. protected array|ArrayAccess $serverData;
  24. /**
  25. * Default fields
  26. *
  27. * Array is structured as [key in record.extra => key in $serverData]
  28. *
  29. * @var array<string, string>
  30. */
  31. protected array $extraFields = [
  32. 'url' => 'REQUEST_URI',
  33. 'ip' => 'REMOTE_ADDR',
  34. 'http_method' => 'REQUEST_METHOD',
  35. 'server' => 'SERVER_NAME',
  36. 'referrer' => 'HTTP_REFERER',
  37. 'user_agent' => 'HTTP_USER_AGENT',
  38. ];
  39. /**
  40. * @param array<string, mixed>|ArrayAccess<string, mixed>|null $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data
  41. * @param array<string, string>|array<string>|null $extraFields Field names and the related key inside $serverData to be added (or just a list of field names to use the default configured $serverData mapping). If not provided it defaults to: [url, ip, http_method, server, referrer] + unique_id if present in server data
  42. */
  43. public function __construct(array|ArrayAccess|null $serverData = null, array|null $extraFields = null)
  44. {
  45. if (null === $serverData) {
  46. $this->serverData = &$_SERVER;
  47. } else {
  48. $this->serverData = $serverData;
  49. }
  50. $defaultEnabled = ['url', 'ip', 'http_method', 'server', 'referrer'];
  51. if (isset($this->serverData['UNIQUE_ID'])) {
  52. $this->extraFields['unique_id'] = 'UNIQUE_ID';
  53. $defaultEnabled[] = 'unique_id';
  54. }
  55. if (null === $extraFields) {
  56. $extraFields = $defaultEnabled;
  57. }
  58. if (isset($extraFields[0])) {
  59. foreach (array_keys($this->extraFields) as $fieldName) {
  60. if (!\in_array($fieldName, $extraFields, true)) {
  61. unset($this->extraFields[$fieldName]);
  62. }
  63. }
  64. } else {
  65. $this->extraFields = $extraFields;
  66. }
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. public function __invoke(LogRecord $record): LogRecord
  72. {
  73. // skip processing if for some reason request data
  74. // is not present (CLI or wonky SAPIs)
  75. if (!isset($this->serverData['REQUEST_URI'])) {
  76. return $record;
  77. }
  78. $record->extra = $this->appendExtraFields($record->extra);
  79. return $record;
  80. }
  81. /**
  82. * @return $this
  83. */
  84. public function addExtraField(string $extraName, string $serverName): self
  85. {
  86. $this->extraFields[$extraName] = $serverName;
  87. return $this;
  88. }
  89. /**
  90. * @param mixed[] $extra
  91. * @return mixed[]
  92. */
  93. private function appendExtraFields(array $extra): array
  94. {
  95. foreach ($this->extraFields as $extraName => $serverName) {
  96. $extra[$extraName] = $this->serverData[$serverName] ?? null;
  97. }
  98. return $extra;
  99. }
  100. }