Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services
https://seldaek.github.io/monolog/
MIT License
20.95k stars 1.9k forks source link

DatetimeImmutable #1881

Closed Misosooup closed 4 months ago

Misosooup commented 5 months ago

Monolog version 1|2|3?

Write your question here.

Hi,

In the Logger.php, there is a function called addRecord which takes in DateTimeImmutable. This object is an object of the Monolog package.

In that function, you have stated that this argument is log date to log in the past or the future. However, when creating the Monolog DateTimeImmutable object, there is no way to set the date for this object. It always default to now. There is no way for me to pass through the base DateTimeImmutable object to the addRecord function as it is strict typed to Monolog\DateTimeImmutable.

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

        parent::__construct('now', $timezone);
    }
public function addRecord(int $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool

I feel like there is some inconsistency here as the function allows a future or a past date, yet the DateTimeImmutable object itself doesn't. Are we meant to use it like this?

Seldaek commented 4 months ago

The way to do it is (new DateTimeImmutable(true))->modify('2022-03-04 05:06:07') if you want to set the date to something specific.