Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/clock interface implementation #1939

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"require": {
"php": ">=8.1",
"psr/clock": "^1.0",
"psr/log": "^2.0 || ^3.0"
},
"require-dev": {
Expand Down
10 changes: 7 additions & 3 deletions src/Monolog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LogLevel;
use Psr\Clock\ClockInterface;
use Throwable;
use Stringable;
use WeakMap;
Expand Down Expand Up @@ -148,6 +149,8 @@ class Logger implements LoggerInterface, ResettableInterface

protected Closure|null $exceptionHandler = null;

protected ClockInterface|null $clock = null;

/**
* Keeps track of depth to prevent infinite logging loops
*/
Expand All @@ -169,16 +172,17 @@ class Logger implements LoggerInterface, ResettableInterface
* @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 for timestamp generation
* @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|null $clock = null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should accept any clock in here or not.. Because a non-monolog clock will output regular DateTime instances, that won't play well with json serialization, and this might even get autowired in some cases, so I find this quite worrying, unless we allow a standard clock to be decorated by the LoggerClock (e.g. with $loggerClock->setClock(ClockInterface $clock) it could use a regular ClockInterface to get the time and copy it into a JsonSerializableDateTimeImmutable instance).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LogRecord does not guarantee a JsonSerializableDateTimeImmutable is used, and $dateTime in addRecord does not enforce it either.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's not enforced in the LogRecord, but it is enforced in addRecord, so it is de-facto always using that datetime class

public function addRecord(int|Level $level, string $message, array $context = [], JsonSerializableDateTimeImmutable|null $datetime = null): bool

{
$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 @@ -357,7 +361,7 @@ public function addRecord(int|Level $level, string $message, array $context = []
$recordInitialized = \count($this->processors) === 0;

$record = new LogRecord(
datetime: $datetime ?? new JsonSerializableDateTimeImmutable($this->microsecondTimestamps, $this->timezone),
datetime: $datetime ?? ($this->clock instanceof ClockInterface ? $this->clock->now() : new JsonSerializableDateTimeImmutable($this->microsecondTimestamps, $this->timezone)),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
datetime: $datetime ?? ($this->clock instanceof ClockInterface ? $this->clock->now() : new JsonSerializableDateTimeImmutable($this->microsecondTimestamps, $this->timezone)),
datetime: $datetime ?? $this->clock?->now() ?? new JsonSerializableDateTimeImmutable($this->microsecondTimestamps, $this->timezone),

channel: $this->name,
level: self::toMonologLevel($level),
message: $message,
Expand Down
39 changes: 39 additions & 0 deletions src/Monolog/LoggerClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace Monolog;

use Psr\Clock\ClockInterface;
use Monolog\JsonSerializableDateTimeImmutable;

class LoggerClock implements ClockInterface
{
private bool $useMicroseconds;
private ?\DateTimeZone $timezone;
private JsonSerializableDateTimeImmutable $fixedTime;

public function __construct(bool $useMicroseconds = true, ?\DateTimeZone $timezone = null)
{
$this->useMicroseconds = $useMicroseconds;
$this->timezone = $timezone;
}

public function now(): JsonSerializableDateTimeImmutable
{
return $this->fixedTime ?? new JsonSerializableDateTimeImmutable($this->useMicroseconds, $this->timezone);
}

public function setUseMicroseconds(bool $useMicroseconds): void
{
$this->useMicroseconds = $useMicroseconds;
}

public function setTimezone(?\DateTimeZone $timezone): void
{
$this->timezone = $timezone;
}

public function setFixedTime(JsonSerializableDateTimeImmutable $fixedTime): void
{
$this->fixedTime = $fixedTime;
}
}