0 follower

Final Class Yiisoft\Profiler\Target\FileTarget

InheritanceYiisoft\Profiler\Target\FileTarget » Yiisoft\Profiler\Target\AbstractTarget
ImplementsYiisoft\Profiler\Target\TargetInterface

FileTarget records profiling messages in a file specified via {@see FileTarget::$filePath}.

Protected Methods

Hide inherited methods

Method Description Defined By
filterMessages() Filters the given messages according to their categories. Yiisoft\Profiler\Target\AbstractTarget

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $filePath, float $requestBeginTime, integer $directoryMode 0775 )
$filePath string

Path of the file to write to. It may contain the placeholders, which will be replaced by computed values. The supported placeholders are:

  • '{ts}' - profiling completion timestamp.
  • '{date}' - profiling completion date in format 'ymd'.
  • '{time}' - profiling completion time in format 'His'.

The directory containing the file will be automatically created if not existing. If target file is already exist it will be overridden.

$requestBeginTime float

Time of the beginning of the request. Can be set as microtime(true) or $_SERVER['REQUEST_TIME_FLOAT'] in config.

$directoryMode integer

The permission to be set for newly created directories. This value will be used by PHP chmod() function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.

                public function __construct(
    private readonly string $filePath,
    private readonly float $requestBeginTime,
    private readonly int $directoryMode = 0775
) {
}

            
collect() public method

Defined in: Yiisoft\Profiler\Target\AbstractTarget::collect()

Processes the given log messages.

This method will filter the given messages with {@see \Yiisoft\Profiler\Target\include()} and {@see \Yiisoft\Profiler\Target\exclude()}. And if requested, it will also export the filtering result to specific medium (e.g. email).

public void collect ( array $messages )
$messages array

Profiling messages to be processed.

Each message has the following keys:

  • token: string, profiling token.
  • level: string, message category.
  • beginTime: float, profiling begin timestamp obtained by microtime(true).
  • endTime: float, profiling end timestamp obtained by microtime(true).
  • duration: float, profiling block duration in milliseconds.
  • beginMemory: int, memory usage at the beginning of profile block in bytes, obtained by memory_get_usage().
  • endMemory: int, memory usage at the end of profile block in bytes, obtained by memory_get_usage().
  • memoryDiff: int, a diff between 'endMemory' and 'beginMemory'.

                public function collect(array $messages): void
{
    if (!$this->enabled) {
        return;
    }
    $messages = $this->filterMessages($messages);
    if (count($messages) > 0) {
        $this->export($messages);
    }
}

            
enable() public method

Defined in: Yiisoft\Profiler\Target\AbstractTarget::enable()

Enable or disable target.

public void enable ( boolean $value true )
$value boolean

                public function enable(bool $value = true): void
{
    $this->enabled = $value;
}

            
exclude() public method
public $this exclude ( string[] $exclude )
$exclude string[]

List of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages.

If this property is not empty, then any category listed here will be excluded from {@see \Yiisoft\Profiler\Target\include()}. You can use an asterisk at the end of a category so that the category can be used to match those categories sharing the same common prefix. For example, 'Yiisoft\Db**' will match categories starting with 'Yiisoft\Db\', such as Yiisoft\Db\Connection.

                public function exclude(array $exclude): self
{
    $new = clone $this;
    $new->exclude = $exclude;
    return $new;
}

            
export() public method

public void export ( array $messages )
$messages array

                public function export(array $messages): void
{
    $memoryPeakUsage = memory_get_peak_usage();
    $totalTime = microtime(true) - $this->requestBeginTime;
    $text = "Total processing time: $totalTime ms; Peak memory: $memoryPeakUsage B. \n\n";
    $text .= implode("\n", array_map([$this, 'formatMessage'], $messages));
    $filename = $this->resolveFilename();
    if (file_exists($filename)) {
        FileHelper::unlink($filename);
    } else {
        $filePath = dirname($filename);
        if (!is_dir($filePath)) {
            FileHelper::ensureDirectory($filePath, $this->directoryMode);
        }
    }
    file_put_contents($filename, $text);
}

            
filterMessages() protected method

Defined in: Yiisoft\Profiler\Target\AbstractTarget::filterMessages()

Filters the given messages according to their categories.

protected Yiisoft\Profiler\Message[] filterMessages ( Yiisoft\Profiler\Message[] $messages )
$messages Yiisoft\Profiler\Message[]

Messages to be filtered. The message structure follows that in {@see \Yiisoft\Profiler\Target\TargetInterface::collect()}.

return Yiisoft\Profiler\Message[]

The filtered messages.

                protected function filterMessages(array $messages): array
{
    foreach ($messages as $i => $message) {
        if (!$this->isCategoryMatched($message->level())) {
            unset($messages[$i]);
        }
    }
    return $messages;
}

            
include() public method

Defined in: Yiisoft\Profiler\Target\AbstractTarget::include()

See also \Yiisoft\Strings\WildcardPattern.

public $this include ( string[] $include )
$include string[]

List of message categories that this target is interested in. Defaults to empty, meaning all categories.

You can use an asterisk at the end of a category so that the category may be used to match those categories sharing the same common prefix. For example, 'Yiisoft\Db**' will match categories starting with 'Yiisoft\Db\', such as Yiisoft\Db\Connection.

                public function include(array $include): self
{
    $new = clone $this;
    $new->include = $include;
    return $new;
}

            
isEnabled() public method

Defined in: Yiisoft\Profiler\Target\AbstractTarget::isEnabled()

Returns target is enabled.

public boolean isEnabled ( )

                public function isEnabled(): bool
{
    return $this->enabled;
}