PHP-Fehlerlog in Datei ausserhalb des Web-Roots schreiben und bei 5 MB drehen
This commit is contained in:
parent
376b940baa
commit
fa7ddc78e1
4 changed files with 101 additions and 0 deletions
52
tippspiel/src/Core/ErrorLog.php
Normal file
52
tippspiel/src/Core/ErrorLog.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Verwaltet die PHP-Fehlerdatei außerhalb des Web-Roots.
|
||||
*
|
||||
* Das Ziel selbst setzt public/.user.ini (PHP-FPM). Diese Klasse kennt
|
||||
* denselben Pfad, damit die Datei im täglichen Reminder-Lauf begrenzt wird –
|
||||
* all-inkl rotiert eigene Dateien im Account nicht.
|
||||
*/
|
||||
class ErrorLog {
|
||||
/** Muss zu error_log in public/.user.ini passen. */
|
||||
const PATH = '/www/htdocs/w0172dd3/php-error.log';
|
||||
|
||||
/** Ab dieser Größe wird die Datei weggedreht. */
|
||||
const MAX_BYTES = 5242880; // 5 MB
|
||||
|
||||
/**
|
||||
* Pfad zur Fehlerdatei. Über PHP_ERROR_LOG überschreibbar (z. B. lokal,
|
||||
* wo /www/htdocs nicht existiert).
|
||||
*/
|
||||
public static function path() {
|
||||
$path = getenv('PHP_ERROR_LOG');
|
||||
return $path !== false && $path !== '' ? $path : self::PATH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dreht die Fehlerdatei weg, sobald sie die Höchstgröße erreicht.
|
||||
* Eine ältere Sicherung wird dabei ersetzt, es bleibt also höchstens
|
||||
* eine Datei mit der Endung .1.
|
||||
*
|
||||
* @param int $maxBytes Grenze in Bytes
|
||||
* @return bool true, wenn rotiert wurde
|
||||
*/
|
||||
public static function rotateIfNeeded($maxBytes = self::MAX_BYTES) {
|
||||
$path = self::path();
|
||||
|
||||
clearstatcache(true, $path);
|
||||
if (!is_file($path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$size = @filesize($path);
|
||||
if ($size === false || $size < $maxBytes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@unlink($path . '.1');
|
||||
return @rename($path, $path . '.1');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue