LogRecord.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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;
  11. use ArrayAccess;
  12. /**
  13. * Monolog log record
  14. *
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. * @template-implements ArrayAccess<'message'|'level'|'context'|'level_name'|'channel'|'datetime'|'extra', int|string|\DateTimeImmutable|array<mixed>>
  17. */
  18. class LogRecord implements ArrayAccess
  19. {
  20. private const MODIFIABLE_FIELDS = [
  21. 'extra' => true,
  22. 'formatted' => true,
  23. ];
  24. public function __construct(
  25. public readonly \DateTimeImmutable $datetime,
  26. public readonly string $channel,
  27. public readonly Level $level,
  28. public readonly string $message,
  29. /** @var array<mixed> */
  30. public readonly array $context = [],
  31. /** @var array<mixed> */
  32. public array $extra = [],
  33. public mixed $formatted = null,
  34. ) {
  35. }
  36. public function offsetSet(mixed $offset, mixed $value): void
  37. {
  38. if ($offset === 'extra') {
  39. if (!\is_array($value)) {
  40. throw new \InvalidArgumentException('extra must be an array');
  41. }
  42. $this->extra = $value;
  43. return;
  44. }
  45. if ($offset === 'formatted') {
  46. $this->formatted = $value;
  47. return;
  48. }
  49. throw new \LogicException('Unsupported operation: setting '.$offset);
  50. }
  51. public function offsetExists(mixed $offset): bool
  52. {
  53. if ($offset === 'level_name') {
  54. return true;
  55. }
  56. return isset($this->{$offset});
  57. }
  58. public function offsetUnset(mixed $offset): void
  59. {
  60. throw new \LogicException('Unsupported operation');
  61. }
  62. public function &offsetGet(mixed $offset): mixed
  63. {
  64. if ($offset === 'level_name' || $offset === 'level') {
  65. // avoid returning readonly props by ref as this is illegal
  66. if ($offset === 'level_name') {
  67. $copy = $this->level->getName();
  68. } else {
  69. $copy = $this->level->value;
  70. }
  71. return $copy;
  72. }
  73. if (isset(self::MODIFIABLE_FIELDS[$offset])) {
  74. return $this->{$offset};
  75. }
  76. // avoid returning readonly props by ref as this is illegal
  77. $copy = $this->{$offset};
  78. return $copy;
  79. }
  80. /**
  81. * @phpstan-return array{message: string, context: mixed[], level: value-of<Level::VALUES>, level_name: value-of<Level::NAMES>, channel: string, datetime: \DateTimeImmutable, extra: mixed[]}
  82. */
  83. public function toArray(): array
  84. {
  85. return [
  86. 'message' => $this->message,
  87. 'context' => $this->context,
  88. 'level' => $this->level->value,
  89. 'level_name' => $this->level->getName(),
  90. 'channel' => $this->channel,
  91. 'datetime' => $this->datetime,
  92. 'extra' => $this->extra,
  93. ];
  94. }
  95. public function with(mixed ...$args): self
  96. {
  97. foreach (['message', 'context', 'level', 'channel', 'datetime', 'extra'] as $prop) {
  98. $args[$prop] ??= $this->{$prop};
  99. }
  100. return new self(...$args);
  101. }
  102. }