Final Class Yiisoft\Log\Target\File\FileTarget
| Inheritance | Yiisoft\ |
|---|
FileTarget records log messages in a file.
The log file is specified via {@see \
If {@see \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| export() | Yiisoft\ |
Method Details
| public mixed __construct ( string $logFile = '/tmp/app.log', Yiisoft\ | ||
| $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\ |
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 \ |
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);
}
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.