Final Class Yiisoft\Log\Message\Formatter
| Inheritance | Yiisoft\ |
|---|
Formatter formats log messages.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| format() | Formats a log message for display as a string. | Yiisoft\ |
| setFormat() | Sets the format for the string representation of the log message. | Yiisoft\ |
| setPrefix() | Sets a PHP callable that returns a string to be prefixed to every exported message. | Yiisoft\ |
| setTimestampFormat() | Sets a date format for the log timestamp. | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_TIMESTAMP_FORMAT | 'Y-m-d H:i:s.u' | Yiisoft\ |
Method Details
| public mixed __construct ( callable|null $format = null, callable|null $prefix = null, string|null $timestampFormat = null ) | ||
| $format | callable|null |
A PHP callable that returns a string representation of the log message.
If not set, {@see \ |
| $prefix | callable|null |
A PHP callable that returns a string to be prefixed to every exported message.
If not set, {@see \ |
| $timestampFormat | string|null |
The date format for the log timestamp. Defaults to |
public function __construct(
private $format = null,
private $prefix = null,
private ?string $timestampFormat = null,
) {}
Formats a log message for display as a string.
| public string format ( Yiisoft\ | ||
| $message | Yiisoft\ |
The log message to be formatted. |
| $commonContext | array |
The user parameters in the |
| return | string |
The formatted log message. |
|---|---|---|
| throws | RuntimeException |
for a callable "format" that does not return a string. |
public function format(Message $message, array $commonContext): string
{
if ($this->format === null) {
return $this->defaultFormat($message, $commonContext);
}
$formatted = ($this->format)($message, $commonContext);
if (!is_string($formatted)) {
throw new RuntimeException(sprintf(
'The PHP callable "format" must return a string, %s received.',
get_debug_type($formatted),
));
}
return $this->getPrefix($message, $commonContext) . $formatted;
}
Sets the format for the string representation of the log message.
See also \
| public void setFormat ( callable $format ) | ||
| $format | callable |
The PHP callable to get a string representation of the log message. |
public function setFormat(callable $format): void
{
$this->format = $format;
}
Sets a PHP callable that returns a string to be prefixed to every exported message.
See also \
| public void setPrefix ( callable $prefix ) | ||
| $prefix | callable |
The PHP callable to get a string prefix of the log message. |
public function setPrefix(callable $prefix): void
{
$this->prefix = $prefix;
}
Sets a date format for the log timestamp.
See also \
| public void setTimestampFormat ( string $timestampFormat ) | ||
| $timestampFormat | string |
The date format for the log timestamp. |
public function setTimestampFormat(string $timestampFormat): void
{
$this->timestampFormat = $timestampFormat;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.