Bevor gemeckert wird:
ini_set( 'error_log', '/tmp/php-error.log' );
user_error( 'Huch! Was passiert', E_USER_Notice );
kenne ich natürlich. Einen Ersatz für Logrotate braucht es natürlich trotzdem.
Hier ein unvollständiger Ansatz zum Testen:
<?php
class phpLogger {
var $logDir = '/tmp';
var $logName = '404.log';
var $maxFileSize = 60; #zum Testen
#var $maxFileSize = 10485760; #10MB
var $maxOldLogs = 3;
var $logStyle = false; # false: error_log(text) / E_USER_NOTICE ... E_USER_ERROR
var $logFile = false;
function __construct() {
if ( ! is_writable( $this -> logDir ) ) {
trigger_error( 'Das Verzeichnis "' . $this -> logDir . '" existiert nicht.', E_USER_WARNING );
$this -> enabled = false;
return false;
}
if ( ! is_writable( $this -> logDir ) ) {
trigger_error( 'Das Verzeichnis "' . $this -> logDir . '"ist nicht beschreibbar.', E_USER_WARNING );
$this -> enabled = false;
return false;
}
$this -> logFile = $this -> logDir . '/' . $this -> logName;
if (
is_file ( $this -> logFile )
and filesize ( $this -> logFile ) >= $this -> maxFileSize
) {
if ( 0 == $this -> maxOldLogs ) {
file_put_contents( $this -> logFile, '' );
return true;
} else {
$f = $this -> logFile . '.' . ( $this -> maxOldLogs ) . 'gz';
if ( is_file ( $f ) ) { unlink( $f ); }
}
if ( 0 < $this -> maxOldLogs ) {
for ( $i = $this -> maxOldLogs -1 ; $i > 0; $i-- ) {
if ( $i > 1 ) {
$o = $this -> logFile . '.' . strval( $i - 1 ) . '.gz';
} else {
$o = $this -> logFile . '.gz';
}
$n = $f = $this -> logFile . '.' . strval( $i ) . '.gz';
if (is_file ( $o ) ) {
rename ( $o, $n );
}
}
}
$sys = 'gzip ' . escapeshellarg( $this -> logFile );
exec( $sys );
file_put_contents( $this -> logFile, '' );
}
}
function log( $s ) {
if ( $this -> logFile ) {
if ( NULL == $this -> logStyle ) {
error_log (
trim( $s ) . PHP_EOL,
3,
$this -> logFile
);
} else {
ini_set( 'error_log', $this -> logFile );
user_error( trim( $s ), E_USER_NOTICE );
}
}
}
}
$logger = new phpLogger();
$logger -> log( 'Hallo Welt' );