0 follower

Final Class Yiisoft\Log\PsrTarget

InheritanceYiisoft\Log\PsrTarget » Yiisoft\Log\Target

PsrTarget is a log target which simply passes messages to another PSR-3 compatible logger.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Sets the PSR-3 logger used to save messages of this target. Yiisoft\Log\PsrTarget
collect() Processes the given log messages. Yiisoft\Log\Target
disable() Disables the log target. Yiisoft\Log\Target
enable() Enables the log target. Yiisoft\Log\Target
getLogger() Yiisoft\Log\PsrTarget
isEnabled() Check whether the log target is enabled. Yiisoft\Log\Target
setCategories() Sets a list of log message categories that this target is interested in. Yiisoft\Log\Target
setCommonContext() Sets a user parameters in the key => value format that should be logged in a each message. Yiisoft\Log\Target
setEnabled() Sets a PHP callable that returns a boolean indicating whether this log target is enabled. Yiisoft\Log\Target
setExcept() Sets a list of log message categories that this target is NOT interested in. Yiisoft\Log\Target
setExportInterval() Sets how many messages should be accumulated before they are exported. Yiisoft\Log\Target
setFormat() Sets a PHP callable that returns a string representation of the log message. Yiisoft\Log\Target
setLevels() Sets a list of {@see \Psr\Log\LogLevel log message levels} that current target is interested in. Yiisoft\Log\Target
setPrefix() Sets a PHP callable that returns a string to be prefixed to every exported message. Yiisoft\Log\Target
setTimestampFormat() Sets a date format for the log timestamp. Yiisoft\Log\Target

Protected Methods

Hide inherited methods

Method Description Defined By
export() Yiisoft\Log\PsrTarget
formatMessages() Formats all log messages for display as a string. Yiisoft\Log\Target
getCommonContext() Gets a user parameters in the key => value format that should be logged in a each message. Yiisoft\Log\Target
getFormattedMessages() Gets a list of formatted log messages. Yiisoft\Log\Target
getMessages() Gets a list of log messages that are retrieved from the logger so far by this log target. Yiisoft\Log\Target

Method Details

Hide inherited methods

__construct() public method

Sets the PSR-3 logger used to save messages of this target.

public mixed __construct ( \Psr\Log\LoggerInterface $logger, string[] $levels = [] )
$logger \Psr\Log\LoggerInterface

The logger instance to be used for messages processing.

$levels string[]

The {@see \Psr\Log\LogLevel log message levels} that this target is interested in.

                public function __construct(private LoggerInterface $logger, array $levels = [])
{
    parent::__construct($levels);
}

            
collect() public method

Defined in: Yiisoft\Log\Target::collect()

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\Log\Message[] $messages, boolean $final )
$messages Yiisoft\Log\Message[]

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 = [];
    }
}

            
disable() public method

Defined in: Yiisoft\Log\Target::disable()

Disables the log target.

See also \Yiisoft\Log\Target::$enabled.

public self disable ( )

                public function disable(): self
{
    $this->enabled = false;
    return $this;
}

            
enable() public method

Defined in: Yiisoft\Log\Target::enable()

Enables the log target.

See also \Yiisoft\Log\Target::$enabled.

public self enable ( )

                public function enable(): self
{
    $this->enabled = true;
    return $this;
}

            
export() protected method

protected void export ( )

                protected function export(): void
{
    foreach ($this->getMessages() as $message) {
        /** @var array $context */
        $context = $message->context();
        $this->logger->log($message->level(), $message->message(), $context);
    }
}

            
formatMessages() protected method

Defined in: Yiisoft\Log\Target::formatMessages()

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;
}

            
getCommonContext() protected method
Deprecated since 2.1, to be removed in 3.0. Use {@see \Yiisoft\Log\ContextProvider\CommonContextProvider} instead.

Defined in: Yiisoft\Log\Target::getCommonContext()

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 key => value format.

                protected function getCommonContext(): array
{
    return $this->commonContext;
}

            
getFormattedMessages() protected method

Defined in: Yiisoft\Log\Target::getFormattedMessages()

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;
}

            
getLogger() public method

public \Psr\Log\LoggerInterface getLogger ( )
return \Psr\Log\LoggerInterface

The logger instance.

                public function getLogger(): LoggerInterface
{
    return $this->logger;
}

            
getMessages() protected method

Defined in: Yiisoft\Log\Target::getMessages()

Gets a list of log messages that are retrieved from the logger so far by this log target.

protected Yiisoft\Log\Message[] getMessages ( )
return Yiisoft\Log\Message[]

The list of log messages.

                protected function getMessages(): array
{
    return $this->messages;
}

            
isEnabled() public method

Defined in: Yiisoft\Log\Target::isEnabled()

Check whether the log target is enabled.

See also \Yiisoft\Log\Target::$enabled.

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;
}

            
setCategories() public method

Defined in: Yiisoft\Log\Target::setCategories()

Sets a list of log message categories that this target is interested in.

See also \Yiisoft\Log\Message\CategoryFilter::$include.

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;
}

            
setCommonContext() public method
Deprecated since 2.1, to be removed in 3.0. Use {@see \Yiisoft\Log\ContextProvider\CommonContextProvider} instead.

Defined in: Yiisoft\Log\Target::setCommonContext()

Sets a user parameters in the key => value format that should be logged in a each message.

See also \Yiisoft\Log\Target::$commonContext.

public self setCommonContext ( array $commonContext )
$commonContext array

The user parameters in the key => value format.

                public function setCommonContext(array $commonContext): self
{
    $this->commonContext = $commonContext;
    return $this;
}

            
setEnabled() public method

Defined in: Yiisoft\Log\Target::setEnabled()

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 \Yiisoft\Log\Target::$enabled.

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;
}

            
setExcept() public method

Defined in: Yiisoft\Log\Target::setExcept()

Sets a list of log message categories that this target is NOT interested in.

See also \Yiisoft\Log\Message\CategoryFilter::$exclude.

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;
}

            
setExportInterval() public method

Defined in: Yiisoft\Log\Target::setExportInterval()

Sets how many messages should be accumulated before they are exported.

See also \Yiisoft\Log\Target::$exportInterval.

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;
}

            
setFormat() public method

Defined in: Yiisoft\Log\Target::setFormat()

Sets a PHP callable that returns a string representation of the log message.

See also \Yiisoft\Log\Message\Formatter::$format.

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;
}

            
setLevels() public method

Defined in: Yiisoft\Log\Target::setLevels()

Sets a list of {@see \Psr\Log\LogLevel log message levels} that current target is interested in.

See also \Yiisoft\Log\Target::$levels.

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 $key => $level) {
        Logger::assertLevelIsValid($level);
        $levels[$key] = $level;
    }
    $this->levels = $levels;
    return $this;
}

            
setPrefix() public method

Defined in: Yiisoft\Log\Target::setPrefix()

Sets a PHP callable that returns a string to be prefixed to every exported message.

See also \Yiisoft\Log\Message\Formatter::$prefix.

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;
}

            
setTimestampFormat() public method

Defined in: Yiisoft\Log\Target::setTimestampFormat()

Sets a date format for the log timestamp.

See also \Yiisoft\Log\Target::$timestampFormat.

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;
}