Final Class Yiisoft\Log\Target\File\FileTarget
| Inheritance | Yiisoft\Log\Target\File\FileTarget » Yiisoft\Log\Target |
|---|
FileTarget records log messages in a file.
The log file is specified via {@see \Yiisoft\Log\Target\File\FileTarget::$logFile}.
If {@see \Yiisoft\Log\Target\File\FileRotator} is used and the size of the log file exceeds {@see \Yiisoft\Log\Target\File\FileRotator::$maxFileSize}, a rotation will be performed, which renames the current log file by suffixing the file name with '.1'. All existing log files are moved backwards by one place, i.e., '.2' to '.3', '.1' to '.2', and so on. If compression is enabled {@see \Yiisoft\Log\Target\File\FileRotator::$compressRotatedFiles}, the rotated files will be compressed into the '.gz' format. The property {@see \Yiisoft\Log\Target\File\FileRotator::$maxFiles} specifies how many history files to keep.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Log\Target\File\FileTarget |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| export() | Yiisoft\Log\Target\File\FileTarget |
Method Details
| public mixed __construct ( string $logFile = '/tmp/app.log', Yiisoft\Log\Target\File\FileRotatorInterface|null $rotator = null, integer $dirMode = 0775, integer|null $fileMode = null, string[] $levels = [] ) | ||
| $logFile | string |
The log file path. If not set, it will use the "/tmp/app.log" file. The directory containing the log files will be automatically created if not existing. |
| $rotator | Yiisoft\Log\Target\File\FileRotatorInterface|null |
The instance that takes care of rotating files. |
| $dirMode | integer |
The permission to be set for newly created directories. This value will be used by PHP
|
| $fileMode | integer|null |
The permission to be set for newly created log files. This value will be used by PHP
|
| $levels | string[] |
The {@see \Psr\Log\LogLevel log message levels} that this target is interested in. |
public function __construct(
private string $logFile = '/tmp/app.log',
private ?FileRotatorInterface $rotator = null,
private int $dirMode = 0775,
private ?int $fileMode = null,
array $levels = []
) {
parent::__construct($levels);
}
| protected void export ( ) |
protected function export(): void
{
$logPath = dirname($this->logFile);
if (!file_exists($logPath)) {
FileHelper::ensureDirectory($logPath, $this->dirMode);
}
$text = $this->formatMessages("\n");
$filePointer = FileHelper::openFile($this->logFile, 'ab');
flock($filePointer, LOCK_EX);
if ($this->rotator !== null) {
// clear stat cache to ensure getting the real current file size and not a cached one
// this may result in rotating twice when cached file size is used on subsequent calls
clearstatcache();
}
if ($this->rotator !== null && $this->rotator->shouldRotateFile($this->logFile)) {
flock($filePointer, LOCK_UN);
fclose($filePointer);
$this->rotator->rotateFile($this->logFile);
$writeResult = file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
} else {
$writeResult = fwrite($filePointer, $text);
flock($filePointer, LOCK_UN);
fclose($filePointer);
}
$this->checkWrittenResult($writeResult, $text);
if ($this->fileMode !== null) {
chmod($this->logFile, $this->fileMode);
}
}
Signup or Login in order to comment.