mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-07 15:57:35 +01:00
* added pcntl to requirements check
* Fix #13920: Fixed erroneous validation for specific cases
* Fix #13920: Added my name to CHANGELOG.md
* trim(): Passing null to parameter #1 ($string) of type string is deprecated
* Added section about Unsafe Reflection in Security best practices doc (#19948)
Co-authored-by: Bizley <pawel.bizley@gmail.com>
* Update CHANGELOG.md
* Fixed tests.
* Fix #13920: Add unit test
* Update `bower-asset/inputmask`, `bower-asset/punycode`.
* added CHANGELOG line
* Fix order.
* fix: #19978 - Mistake in Korean translation
* fix: keep doublequote
* Do not duplicate log messages in memory
* Update framework/log/FileTarget.php
Co-authored-by: Bizley <pawel.bizley@gmail.com>
* Update concept-di-container.md
Сслка "Конфигурация приложения" была не верной, она вела на страницу "Service-locator'a"
* Update CHANGELOG.md
* release version 2.0.49.1
* prepare for next release
* Revert changes in `mimeTypes.php` from 4a1f2c6b9b
restores https://github.com/yiisoft/yii2/pull/19936
* update actions/checkout to v4
* Fix mime type generator
* Added note
* Update structure-controllers.md (#20003)
добавил пропущенное слово "как"
* Update CHANGELOG.md
2.0.49.2 changelog
* release version 2.0.49.2
* prepare for next release
* New methods: BaseActiveRecord::loadRelations() and BaseActiveRecord::loadRelationsFor().
* Fixed a bug where the yii serve command would break if a custom router was supplied and it had a space in the path
* Fix `MaskedInputAsset::class`.
* Fix #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages
* Added 'zh' into 'framework/messages/config.php' (#19995)
---------
Co-authored-by: Tobias Munk <schmunk@usrbin.de>
Co-authored-by: Tim Fischer <tf@maschinensucher.de>
Co-authored-by: Tim Fischer <35448254+tim-fischer-maschinensucher@users.noreply.github.com>
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
Co-authored-by: Oleg Poludnenko <ua.oleg@gmail.com>
Co-authored-by: Bizley <pawel.bizley@gmail.com>
Co-authored-by: Sonia Zorba <zonia3000@gmail.com>
Co-authored-by: Tobias Munk <t.munk@herzogkommunikation.de>
Co-authored-by: Akbar Herlambang <akbar6393222@yahoo.com>
Co-authored-by: lubosdz <lubosdz@gmail.com>
Co-authored-by: Yuriy Bachevskiy <Materik-r@yandex.ru>
Co-authored-by: Robert Korulczyk <robert@korulczyk.pl>
Co-authored-by: salehhashemi1992 <81674631+salehhashemi1992@users.noreply.github.com>
Co-authored-by: PowerGamer1 <PowerGamer1@users.noreply.github.com>
Co-authored-by: Brad Bell <brad@pixelandtonic.com>
Co-authored-by: Alexandru Trandafir Catalin <alex@heavydots.com>
Co-authored-by: Nabi KaramAliZadeh <NabiKAZ@gmail.com>
180 lines
6.2 KiB
PHP
180 lines
6.2 KiB
PHP
<?php
|
|
/**
|
|
* @link https://www.yiiframework.com/
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
* @license https://www.yiiframework.com/license/
|
|
*/
|
|
|
|
namespace yii\log;
|
|
|
|
use Yii;
|
|
use yii\base\InvalidConfigException;
|
|
use yii\helpers\FileHelper;
|
|
|
|
/**
|
|
* FileTarget records log messages in a file.
|
|
*
|
|
* The log file is specified via [[logFile]]. If the size of the log file exceeds
|
|
* [[maxFileSize]] (in kilo-bytes), 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.
|
|
* The property [[maxLogFiles]] specifies how many history files to keep.
|
|
*
|
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
* @since 2.0
|
|
*/
|
|
class FileTarget extends Target
|
|
{
|
|
/**
|
|
* @var string|null log file path or [path alias](guide:concept-aliases). If not set, it will use the "@runtime/logs/app.log" file.
|
|
* The directory containing the log files will be automatically created if not existing.
|
|
*/
|
|
public $logFile;
|
|
/**
|
|
* @var bool whether log files should be rotated when they reach a certain [[maxFileSize|maximum size]].
|
|
* Log rotation is enabled by default. This property allows you to disable it, when you have configured
|
|
* an external tools for log rotation on your server.
|
|
* @since 2.0.3
|
|
*/
|
|
public $enableRotation = true;
|
|
/**
|
|
* @var int maximum log file size, in kilo-bytes. Defaults to 10240, meaning 10MB.
|
|
*/
|
|
public $maxFileSize = 10240; // in KB
|
|
/**
|
|
* @var int number of log files used for rotation. Defaults to 5.
|
|
*/
|
|
public $maxLogFiles = 5;
|
|
/**
|
|
* @var int|null the permission to be set for newly created log files.
|
|
* This value will be used by PHP chmod() function. No umask will be applied.
|
|
* If not set, the permission will be determined by the current environment.
|
|
*/
|
|
public $fileMode;
|
|
/**
|
|
* @var int 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 $dirMode = 0775;
|
|
|
|
/**
|
|
* Initializes the route.
|
|
* This method is invoked after the route is created by the route manager.
|
|
*/
|
|
public function init()
|
|
{
|
|
parent::init();
|
|
if ($this->logFile === null) {
|
|
$this->logFile = Yii::$app->getRuntimePath() . '/logs/app.log';
|
|
} else {
|
|
$this->logFile = Yii::getAlias($this->logFile);
|
|
}
|
|
if ($this->maxLogFiles < 1) {
|
|
$this->maxLogFiles = 1;
|
|
}
|
|
if ($this->maxFileSize < 1) {
|
|
$this->maxFileSize = 1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Writes log messages to a file.
|
|
* Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
|
|
* @throws InvalidConfigException if unable to open the log file for writing
|
|
* @throws LogRuntimeException if unable to write complete log to file
|
|
*/
|
|
public function export()
|
|
{
|
|
$text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
|
|
|
|
if (trim($text) === '') {
|
|
return; // No messages to export, so we exit the function early
|
|
}
|
|
|
|
if (strpos($this->logFile, '://') === false || strncmp($this->logFile, 'file://', 7) === 0) {
|
|
$logPath = dirname($this->logFile);
|
|
FileHelper::createDirectory($logPath, $this->dirMode, true);
|
|
}
|
|
|
|
if (($fp = @fopen($this->logFile, 'a')) === false) {
|
|
throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
|
|
}
|
|
@flock($fp, LOCK_EX);
|
|
if ($this->enableRotation) {
|
|
// 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->enableRotation && @filesize($this->logFile) > $this->maxFileSize * 1024) {
|
|
$this->rotateFiles();
|
|
}
|
|
$writeResult = @fwrite($fp, $text);
|
|
if ($writeResult === false) {
|
|
$error = error_get_last();
|
|
throw new LogRuntimeException("Unable to export log through file ({$this->logFile})!: {$error['message']}");
|
|
}
|
|
$textSize = strlen($text);
|
|
if ($writeResult < $textSize) {
|
|
throw new LogRuntimeException("Unable to export whole log through file ({$this->logFile})! Wrote $writeResult out of $textSize bytes.");
|
|
}
|
|
@fflush($fp);
|
|
@flock($fp, LOCK_UN);
|
|
@fclose($fp);
|
|
|
|
if ($this->fileMode !== null) {
|
|
@chmod($this->logFile, $this->fileMode);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Rotates log files.
|
|
*/
|
|
protected function rotateFiles()
|
|
{
|
|
$file = $this->logFile;
|
|
for ($i = $this->maxLogFiles; $i >= 0; --$i) {
|
|
// $i == 0 is the original log file
|
|
$rotateFile = $file . ($i === 0 ? '' : '.' . $i);
|
|
if (is_file($rotateFile)) {
|
|
// suppress errors because it's possible multiple processes enter into this section
|
|
if ($i === $this->maxLogFiles) {
|
|
@unlink($rotateFile);
|
|
continue;
|
|
}
|
|
$newFile = $this->logFile . '.' . ($i + 1);
|
|
$this->rotateByCopy($rotateFile, $newFile);
|
|
if ($i === 0) {
|
|
$this->clearLogFile($rotateFile);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/***
|
|
* Clear log file without closing any other process open handles
|
|
* @param string $rotateFile
|
|
*/
|
|
private function clearLogFile($rotateFile)
|
|
{
|
|
if ($filePointer = @fopen($rotateFile, 'a')) {
|
|
@ftruncate($filePointer, 0);
|
|
@fclose($filePointer);
|
|
}
|
|
}
|
|
|
|
/***
|
|
* Copy rotated file into new file
|
|
* @param string $rotateFile
|
|
* @param string $newFile
|
|
*/
|
|
private function rotateByCopy($rotateFile, $newFile)
|
|
{
|
|
@copy($rotateFile, $newFile);
|
|
if ($this->fileMode !== null) {
|
|
@chmod($newFile, $this->fileMode);
|
|
}
|
|
}
|
|
}
|