Abstract Class Yiisoft\Log\Target
| Inheritance | Yiisoft\ |
|---|---|
| Subclasses | Yiisoft\ |
Target is the base class for all log target classes.
A log target object will filter the messages logged by {@see \
Level filter and category filter are combinatorial, i.e., only messages
satisfying both filter conditions will be handled. Additionally, you may
specify {@see \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | When defining a constructor in child classes, you must call parent::__construct(). |
Yiisoft\ |
| collect() | Processes the given log messages. | Yiisoft\ |
| disable() | Disables the log target. | Yiisoft\ |
| enable() | Enables the log target. | Yiisoft\ |
| isEnabled() | Check whether the log target is enabled. | Yiisoft\ |
| setCategories() | Sets a list of log message categories that this target is interested in. | Yiisoft\ |
| setCommonContext() | Sets a user parameters in the key => value format that should be logged in a each message. |
Yiisoft\ |
| setEnabled() | Sets a PHP callable that returns a boolean indicating whether this log target is enabled. | Yiisoft\ |
| setExcept() | Sets a list of log message categories that this target is NOT interested in. | Yiisoft\ |
| setExportInterval() | Sets how many messages should be accumulated before they are exported. | Yiisoft\ |
| setFormat() | Sets a PHP callable that returns a string representation of the log message. | Yiisoft\ |
| setLevels() | Sets a list of {@see LogLevel log message levels} that current target is interested in. | 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\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| export() | Exports log messages to a specific destination. | Yiisoft\ |
| formatMessages() | Formats all log messages for display as a string. | Yiisoft\ |
| getCommonContext() | Gets a user parameters in the key => value format that should be logged in a each message. |
Yiisoft\ |
| getFormattedMessages() | Gets a list of formatted log messages. | Yiisoft\ |
| getMessages() | Gets a list of log messages that are retrieved from the logger so far by this log target. | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_EXPORT_INTERVAL | 1000 | Yiisoft\ |
Method Details
When defining a constructor in child classes, you must call parent::__construct().
| public mixed __construct ( string[] $levels = [], string[] $categories = [], string[] $exceptCategories = [], callable|null $format = null, callable|null $prefix = null, string|null $timestampFormat = null, integer $exportInterval = self::DEFAULT_EXPORT_INTERVAL, boolean|callable $enabled = true ) | ||
| $levels | string[] |
The {@see \ |
| $categories | string[] |
The log message categories that this target is interested in. |
| $exceptCategories | string[] |
The log message categories that this target is NOT interested in. |
| $format | callable|null |
A PHP callable that returns a string representation of the log message. |
| $prefix | callable|null |
A PHP callable that returns a string to be prefixed to every exported message. |
| $timestampFormat | string|null |
The date format for the log timestamp. |
| $exportInterval | integer |
How many messages should be accumulated before they are exported. |
| $enabled | boolean|callable |
Whether this target is enabled, or a PHP callable that returns a boolean. |
public function __construct(
array $levels = [],
array $categories = [],
array $exceptCategories = [],
?callable $format = null,
?callable $prefix = null,
?string $timestampFormat = null,
private int $exportInterval = self::DEFAULT_EXPORT_INTERVAL,
bool|callable $enabled = true,
) {
$this->categories = new CategoryFilter($categories, $exceptCategories);
$this->formatter = new Formatter($format, $prefix, $timestampFormat);
/** @psalm-suppress DeprecatedMethod */
$this->setLevels($levels);
$this->enabled = $enabled;
}
Processes the given log messages.
This method will filter the given messages with levels and categories. And if requested, it will also export the filtering result to specific medium (e.g. email).
| public void collect ( Yiisoft\ | ||
| $messages | Yiisoft\ |
Log messages to be processed. |
| $final | boolean |
Whether this method is called at the end of the current application. |
public function collect(array $messages, bool $final): void
{
$this->filterMessages($messages);
$count = count($this->messages);
if ($count > 0 && ($final || ($this->exportInterval > 0 && $count >= $this->exportInterval))) {
// set exportInterval to 0 to avoid triggering export again while exporting
$oldExportInterval = $this->exportInterval;
$this->exportInterval = 0;
$this->export();
$this->exportInterval = $oldExportInterval;
$this->messages = [];
}
}
Disables the log target.
See also \
| public self disable ( ) |
public function disable(): self
{
$this->enabled = false;
return $this;
}
Enables the log target.
See also \
| public self enable ( ) |
public function enable(): self
{
$this->enabled = true;
return $this;
}
Exports log messages to a specific destination.
Child classes must implement this method.
| protected abstract void export ( ) |
abstract protected function export(): void;
Formats all log messages for display as a string.
| protected string formatMessages ( string $separator = '' ) | ||
| $separator | string |
The log messages string separator. |
| return | string |
The string formatted log messages. |
|---|---|---|
protected function formatMessages(string $separator = ''): string
{
$formatted = '';
foreach ($this->messages as $message) {
$formatted .= $this->formatter->format($message, $this->commonContext) . $separator;
}
return $formatted;
}
Gets a user parameters in the key => value format that should be logged in a each message.
| protected array getCommonContext ( ) | ||
| return | array |
The user parameters in the |
|---|---|---|
protected function getCommonContext(): array
{
return $this->commonContext;
}
Gets a list of formatted log messages.
| protected string[] getFormattedMessages ( ) | ||
| return | string[] |
The list of formatted log messages. |
|---|---|---|
protected function getFormattedMessages(): array
{
$formatted = [];
foreach ($this->messages as $key => $message) {
$formatted[$key] = $this->formatter->format($message, $this->commonContext);
}
return $formatted;
}
Gets a list of log messages that are retrieved from the logger so far by this log target.
| protected Yiisoft\ | ||
| return | Yiisoft\ |
The list of log messages. |
|---|---|---|
protected function getMessages(): array
{
return $this->messages;
}
Check whether the log target is enabled.
See also \
| public boolean isEnabled ( ) | ||
| return | boolean |
The value indicating whether this log target is enabled. |
|---|---|---|
| throws | RuntimeException |
for a callable "enabled" that does not return a boolean. |
public function isEnabled(): bool
{
if (is_bool($this->enabled)) {
return $this->enabled;
}
if (!is_bool($enabled = ($this->enabled)())) {
throw new RuntimeException(sprintf(
'The PHP callable "enabled" must returns a boolean, %s received.',
get_debug_type($enabled),
));
}
return $enabled;
}
$categories constructor parameter instead.
Sets a list of log message categories that this target is interested in.
See also \
| public self setCategories ( string[] $categories ) | ||
| $categories | string[] |
The list of log message categories. |
| throws | InvalidArgumentException |
for invalid log message categories structure. |
|---|---|---|
public function setCategories(array $categories): self
{
$this->categories->include($categories);
return $this;
}
Sets a user parameters in the key => value format that should be logged in a each message.
See also \
| public self setCommonContext ( array $commonContext ) | ||
| $commonContext | array |
The user parameters in the |
public function setCommonContext(array $commonContext): self
{
$this->commonContext = $commonContext;
return $this;
}
$enabled constructor parameter instead.
Sets a PHP callable that returns a boolean indicating whether this log target is enabled.
The signature of the callable should be function (): bool;.
See also \
| public self setEnabled ( callable $value ) | ||
| $value | callable |
The PHP callable to get a boolean value. |
public function setEnabled(callable $value): self
{
$this->enabled = $value;
return $this;
}
$exceptCategories constructor parameter instead.
Sets a list of log message categories that this target is NOT interested in.
See also \
| public self setExcept ( string[] $except ) | ||
| $except | string[] |
The list of log message categories. |
| throws | InvalidArgumentException |
for invalid log message categories structure. |
|---|---|---|
public function setExcept(array $except): self
{
$this->categories->exclude($except);
return $this;
}
$exportInterval constructor parameter instead.
Sets how many messages should be accumulated before they are exported.
See also \
| public self setExportInterval ( integer $exportInterval ) | ||
| $exportInterval | integer |
The number of log messages to accumulate before exporting. |
public function setExportInterval(int $exportInterval): self
{
$this->exportInterval = $exportInterval;
return $this;
}
$format constructor parameter instead.
Sets a PHP callable that returns a string representation of the log message.
See also \
| public self setFormat ( callable $format ) | ||
| $format | callable |
The PHP callable to get a string value from. |
public function setFormat(callable $format): self
{
$this->formatter->setFormat($format);
return $this;
}
$levels constructor parameter instead.
Sets a list of {@see LogLevel log message levels} that current target is interested in.
See also \
| public self setLevels ( string[] $levels ) | ||
| $levels | string[] |
The list of log message levels. |
| throws | InvalidArgumentException |
for invalid log message level. |
|---|---|---|
public function setLevels(array $levels): self
{
foreach ($levels as $level) {
Logger::assertLevelIsValid($level);
}
$this->levels = $levels;
return $this;
}
$prefix constructor parameter instead.
Sets a PHP callable that returns a string to be prefixed to every exported message.
See also \
| public self setPrefix ( callable $prefix ) | ||
| $prefix | callable |
The PHP callable to get a string prefix of the log message. |
public function setPrefix(callable $prefix): self
{
$this->formatter->setPrefix($prefix);
return $this;
}
$timestampFormat constructor parameter instead.
Sets a date format for the log timestamp.
See also \
| public self setTimestampFormat ( string $format ) | ||
| $format | string |
The date format for the log timestamp. |
public function setTimestampFormat(string $format): self
{
$this->formatter->setTimestampFormat($format);
return $this;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.