-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
omerimzali
wants to merge
5
commits into
Seldaek:main
Choose a base branch
from
omerimzali:feature/clock-interface-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0951cb6
Add PSR-20 ClockInterface support for customizable timestamps
omerimzali 570eaba
Add LoggerClock implementation for PSR-20 Clock interface
omerimzali 8301707
Add PSR-20 ClockInterface support for customizable timestamps
omerimzali bb636ff
Add psr/clock to composer.json
omerimzali fc49f1b
Use instanceof check for ClockInterface in Logger
omerimzali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
], | ||
"require": { | ||
"php": ">=8.1", | ||
"psr/clock": "^1.0", | ||
"psr/log": "^2.0 || ^3.0" | ||
}, | ||
"require-dev": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
@@ -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 | ||||||
*/ | ||||||
|
@@ -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) | ||||||
{ | ||||||
$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 | ||||||
|
@@ -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)), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
channel: $this->name, | ||||||
level: self::toMonologLevel($level), | ||||||
message: $message, | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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
inaddRecord
does not enforce it either.There was a problem hiding this comment.
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
monolog/src/Monolog/Logger.php
Line 332 in 548eeb3