Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Message::time() method #114

Merged
merged 3 commits into from
Jul 1, 2024
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
`CategoryFilter::DEFAULT` in favor it (@vjik)
- New #113: Add `Message::trace()` method (@vjik)
- Enh #113: Remove unnecessary `unset` call in `ContextProvider` (@vjik)
- New #114: Add `Message::time()` method (@vjik)

## 2.0.0 May 22, 2022

Expand Down
48 changes: 47 additions & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Yiisoft\Log;

use DateTimeImmutable;
use DateTimeInterface;
use Exception;
use LogicException;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerTrait;
Expand Down Expand Up @@ -48,11 +51,16 @@ final class Message
*
* - category: string, message category.
* - memory: int, memory usage in bytes, obtained by `memory_get_usage()`.
* - time: float, message timestamp obtained by microtime(true).
* - time: float, message timestamp obtained by `microtime(true)`.
* - trace: array, debug backtrace, contains the application code call stacks.
*/
private array $context;

/**
* Default time to use when the time is not set in the context.
*/
private DateTimeImmutable $defaultTime;

/**
* @param string $level Log message level.
* @param string|Stringable $message Log message.
Expand All @@ -69,6 +77,7 @@ public function __construct(string $level, string|Stringable $message, array $co
$this->level = $level;
$this->message = $this->parse($message, $context);
$this->context = $context;
$this->defaultTime = new DateTimeImmutable();
}

/**
Expand Down Expand Up @@ -147,6 +156,43 @@ public function trace(): ?array
return $trace;
}

/**
* Returns the time of the log message.
*
* @return DateTimeImmutable The log message time.
*/
public function time(): DateTimeImmutable
{
$time = $this->context['time'] ?? $this->defaultTime;

if ($time instanceof DateTimeInterface) {
return DateTimeImmutable::createFromInterface($time);
}

if (is_int($time) || is_float($time)) {
try {
return new DateTimeImmutable('@' . $time);
} catch (Exception $e) {
throw new LogicException('Invalid time value in log context: ' . $time . '.', previous: $e);
}
}

if (is_string($time)) {
$format = match (true) {
str_contains($time, '.') => 'U.u',
str_contains($time, ',') => 'U,u',
default => 'U',
};
$date = DateTimeImmutable::createFromFormat($format, $time);
if ($date === false) {
throw new LogicException('Invalid time value in log context: "' . $time . '".');
}
return $date;
}

throw new LogicException('Invalid time value in log context. Got "' . get_debug_type($time) . '".');
}

/**
* Parses log message resolving placeholders in the form: "{foo}",
* where foo will be replaced by the context data in key "foo".
Expand Down
44 changes: 1 addition & 43 deletions src/Message/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace Yiisoft\Log\Message;

use DateTime;
use DateTimeInterface;
use Exception;
use LogicException;
use RuntimeException;
use Yiisoft\Log\Message;
use Yiisoft\VarDumper\VarDumper;
Expand Down Expand Up @@ -124,51 +120,13 @@ public function format(Message $message, array $commonContext): string
*/
private function defaultFormat(Message $message, array $commonContext): string
{
$time = $this->getTime($message);
$time = $message->time()->format($this->timestampFormat);
$prefix = $this->getPrefix($message, $commonContext);
$context = $this->getContext($message, $commonContext);

return "{$time} {$prefix}[{$message->level()}][{$message->category()}] {$message->message()}{$context}";
}

/**
* Gets formatted timestamp for message, according to {@see Formatter::$timestampFormat}.
*
* @param Message $message The log message.
*
* @return string Formatted timestamp for message.
*/
private function getTime(Message $message): string
{
$time = $message->context('time');

if (is_int($time) || is_float($time)) {
try {
$date = new DateTime('@' . $time);
} catch (Exception $e) {
throw new LogicException('Invalid time value in log context: ' . $time . '.', previous: $e);
}
} elseif (is_string($time)) {
$format = match (true) {
str_contains($time, '.') => 'U.u',
str_contains($time, ',') => 'U,u',
default => 'U',
};
$date = DateTime::createFromFormat($format, $time);
if ($date === false) {
throw new LogicException('Invalid time value in log context: "' . $time . '".');
}
} elseif ($time instanceof DateTimeInterface) {
$date = $time;
} elseif ($time === null) {
$date = new DateTime();
} else {
throw new LogicException('Invalid time value in log context. Got "' . get_debug_type($time) . '".');
}

return $date->format($this->timestampFormat);
}

/**
* Gets a string to be prefixed to the given message.
*
Expand Down
Loading