Skip to content

Commit

Permalink
Add optional ClockInterface parameter to Logger constructor. Modify a…
Browse files Browse the repository at this point in the history
…ddRecord to use Clock to create timestamps
  • Loading branch information
omerimzali committed Feb 3, 2025
1 parent 6f961e9 commit 1a52930
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Monolog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
use Psr\Log\LoggerInterface;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LogLevel;
use Psr\Clock\ClockInterface;
use Throwable;
use Stringable;
use WeakMap;


/**
* Monolog log channel
*
Expand Down Expand Up @@ -164,21 +166,28 @@ class Logger implements LoggerInterface, ResettableInterface
*/
private bool $detectCycles = true;

/**
* @var ClockInterface|null
*/
private ?ClockInterface $clock;

/**
* @param string $name The logging channel, a simple descriptive name that is attached to all log records
* @param list<HandlerInterface> $handlers Optional stack of handlers, the first one in the array is called first, etc.
* @param callable[] $processors Optional array of processors
* @param DateTimeZone|null $timezone Optional timezone, if not provided date_default_timezone_get() will be used
* @param ClockInterface|null $clock Optional clock service for fetching timestamps
*
* @phpstan-param array<(callable(LogRecord): LogRecord)|ProcessorInterface> $processors
*/
public function __construct(string $name, array $handlers = [], array $processors = [], DateTimeZone|null $timezone = null)
public function __construct(string $name, array $handlers = [], array $processors = [], DateTimeZone|null $timezone = null, ?ClockInterface $clock = null)
{
$this->name = $name;
$this->setHandlers($handlers);
$this->processors = $processors;
$this->timezone = $timezone ?? new DateTimeZone(date_default_timezone_get());
$this->fiberLogDepth = new \WeakMap();
$this->clock = $clock;
}

public function getName(): string
Expand Down Expand Up @@ -329,7 +338,7 @@ public function useLoggingLoopDetection(bool $detectCycles): self
*
* @phpstan-param value-of<Level::VALUES>|Level $level
*/
public function addRecord(int|Level $level, string $message, array $context = [], JsonSerializableDateTimeImmutable|null $datetime = null): bool
public function addRecord(int|Level $level, string $message, array $context = [], bool $useClock = false): bool

Check failure on line 341 in src/Monolog/Logger.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

PHPDoc tag @param references unknown parameter: $datetime

Check failure on line 341 in src/Monolog/Logger.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

PHPDoc tag @param references unknown parameter: $datetime
{
if (\is_int($level) && isset(self::RFC_5424_LEVELS[$level])) {
$level = self::RFC_5424_LEVELS[$level];
Expand All @@ -356,8 +365,9 @@ public function addRecord(int|Level $level, string $message, array $context = []
try {
$recordInitialized = \count($this->processors) === 0;

$datetime = $useClock ? $this->clock->now() : new JsonSerializableDateTimeImmutable($this->microsecondTimestamps, $this->timezone);

Check failure on line 368 in src/Monolog/Logger.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Cannot call method now() on Psr\Clock\ClockInterface|null.

Check failure on line 368 in src/Monolog/Logger.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Cannot call method now() on Psr\Clock\ClockInterface|null.
$record = new LogRecord(
datetime: $datetime ?? new JsonSerializableDateTimeImmutable($this->microsecondTimestamps, $this->timezone),
datetime: $datetime,
channel: $this->name,
level: self::toMonologLevel($level),
message: $message,
Expand Down Expand Up @@ -732,6 +742,7 @@ public function __serialize(): array
'exceptionHandler' => $this->exceptionHandler,
'logDepth' => $this->logDepth,
'detectCycles' => $this->detectCycles,
'clock' => $this->clock,
];
}

Expand All @@ -740,7 +751,7 @@ public function __serialize(): array
*/
public function __unserialize(array $data): void
{
foreach (['name', 'handlers', 'processors', 'microsecondTimestamps', 'timezone', 'exceptionHandler', 'logDepth', 'detectCycles'] as $property) {
foreach (['name', 'handlers', 'processors', 'microsecondTimestamps', 'timezone', 'exceptionHandler', 'logDepth', 'detectCycles', 'clock'] as $property) {
if (isset($data[$property])) {
$this->$property = $data[$property];
}
Expand Down

0 comments on commit 1a52930

Please sign in to comment.