0 follower

Final Class Yiisoft\Log\StreamTarget

InheritanceYiisoft\Log\StreamTarget » Yiisoft\Log\Target

StreamTarget is the log target that writes to the specified output stream.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Log\StreamTarget
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
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 \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\StreamTarget
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

public __construct( resource|string $stream 'php://stdout', string[] $levels = [] ): mixed
$stream resource|string

A string stream identifier or a stream resource.

$levels string[]

The \Psr\Log\LogLevel log message levels that this target is interested in.

                public function __construct(private $stream = 'php://stdout', 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 collect( Yiisoft\Log\Message[] $messages, boolean $final ): void
$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 disable( ): self

                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 enable( ): self

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

            
export() protected method

protected export( ): void

                protected function export(): void
{
    $stream = $this->createStream();
    flock($stream, LOCK_EX);
    if (fwrite($stream, $this->formatMessages("\n")) === false) {
        flock($stream, LOCK_UN);
        fclose($stream);
        throw new RuntimeException(sprintf(
            'Unable to export the log because of an error writing to the stream: %s',
            error_get_last()['message'] ?? '',
        ));
    }
    $this->stream = stream_get_meta_data($stream)['uri'];
    flock($stream, LOCK_UN);
    fclose($stream);
}

            
formatMessages() protected method

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

Formats all log messages for display as a string.

protected formatMessages( string $separator '' ): string
$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 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 getCommonContext( ): array
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 getFormattedMessages( ): string[]
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;
}

            
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 getMessages( ): Yiisoft\Log\Message[]
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 isEnabled( ): boolean
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 setCategories( string[] $categories ): self
$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 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 setCommonContext( array $commonContext ): self
$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 setEnabled( callable $value ): self
$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 setExcept( string[] $except ): self
$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 setExportInterval( integer $exportInterval ): self
$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 setFormat( callable $format ): self
$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 \Psr\Log\LogLevel log message levels that current target is interested in.

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

public setLevels( string[] $levels ): self
$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 setPrefix( callable $prefix ): self
$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 setTimestampFormat( string $format ): self
$format string

The date format for the log timestamp.

                public function setTimestampFormat(string $format): self
{
    $this->formatter->setTimestampFormat($format);
    return $this;
}