Created
June 22, 2012 12:29
-
-
Save dnaber-de/2972473 to your computer and use it in GitHub Desktop.
yet another log writer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* class Logwriter | |
* | |
* yet another log writer | |
* | |
* @author David Naber <kontakt@dnaber.de> | |
* @charset utf-8 | |
* @version 0.3 | |
* @licence GPLv3 | |
*/ | |
class Logwriter { | |
/** | |
* Linefeed character | |
* | |
* @protected | |
* @static | |
* @var string | |
*/ | |
protected static $lfc = "\r\n"; | |
/** | |
* $directory | |
* | |
* @access protected | |
* @static | |
* @var string | |
*/ | |
protected static $directory = ''; | |
/** | |
* $is_live | |
* | |
* set this to TRUE if you runing on a live system and don't want to | |
* display any internal errors of this class | |
* | |
* @access protected | |
* @static | |
* @var bool | |
*/ | |
protected static $is_live = TRUE; | |
/** | |
* $context | |
* | |
* this will be part of the name of the logfile | |
* | |
* @access protected | |
* @var string | |
*/ | |
protected $context = ''; | |
/** | |
* $extension | |
* | |
* @access protected | |
* @var string | |
*/ | |
protected $extension = ''; | |
/** | |
* $logtext | |
* | |
* @access protected | |
* @var string | |
*/ | |
protected $logtext = ''; | |
/** | |
* $max_filesize | |
* | |
* if exceeded, we try to create a new file | |
* default 1MiB = 2e20 Byte | |
* | |
* @access protected | |
* @var int | |
*/ | |
protected $max_filesize = 1048576; | |
/** | |
* set_lf | |
* | |
* @access public | |
* @static | |
* @return void | |
*/ | |
public static function set_lf ( $lf = "\r\n" ) { | |
self :: $lfc = $lf; | |
} | |
/** | |
* set_directory | |
* | |
* @access public | |
* @static | |
* @param string $directory | |
* @return void | |
*/ | |
public static function set_directory( $directory ) { | |
self :: $directory = rtrim( $directory, '\/' ); | |
} | |
/** | |
* set_live | |
* | |
* @access public | |
* @static | |
* @param bool $live | |
* @return void | |
*/ | |
public static function set_live( $live = TRUE ) { | |
self :: $is_live = ( bool ) $live; | |
} | |
/** | |
* contstructor | |
* | |
* @access public | |
* @param string $context | |
* @param string $extension (optional) | |
* @return Logwriter | |
*/ | |
public function __construct( $context, $extension = '.txt' ) { | |
$this->context = $context; | |
$this->filename = $context . $extension; | |
$this->extension = $extension; | |
} | |
/** | |
* set_max_filesize | |
* @access public | |
* @param int $max_filesize | |
*/ | |
public function set_max_filesize( $max_filesize ) { | |
$this->max_filesize = ( int ) $max_filesize; | |
} | |
/** | |
* addln | |
* | |
* @access public | |
* @param string $logtext | |
* @return void | |
*/ | |
public function addln( $logtext ) { | |
$this->logtext .= $logtext . self :: $lfc; | |
} | |
/** | |
* write | |
* | |
* @access public | |
* @return void | |
*/ | |
public function write() { | |
if ( empty( $this->logtext ) ) { | |
return; | |
} | |
if ( ! is_dir( self :: $directory ) ) { | |
if ( ! self :: $is_live ) { | |
echo 'The given directory "' . self :: $directory . '" does not exists in ' . __METHOD__; | |
} | |
return; | |
} | |
$fp = NULL; | |
//if logfile exists and is writable | |
if ( file_exists( self :: $directory . '/' . $this->filename ) | |
&& is_writable( self :: $directory . '/' . $this->filename ) | |
) { | |
//file exists, but it is to large | |
if( filesize( self :: $directory . '/' . $this->filename ) > $this->max_filesize ) { | |
if ( ! $this->clear_log( TRUE ) && ! self :: $is_live ) { | |
echo "The logfile couldn't archivate in " . __METHOD__; | |
} | |
} | |
//use the existing one | |
$fp = @fopen( self :: $directory . '/' . $this->filename, 'a' ); | |
} | |
//logfile not exists or isn't writable | |
else { | |
//touch new logfile | |
$fp = @fopen( self :: $directory . '/' . $this->filename, 'w' ); | |
} | |
$wrote = FALSE; | |
if( is_resource( $fp ) ) { | |
$wrote = @fwrite( $fp, $this->logtext ); | |
@fclose( $fp ); | |
} | |
if ( FALSE === $wrote && ! self :: $is_live ) { | |
echo nl2br( "Logfile \"" . $this->filename ."\" wasn't written in " . __METHOD__ . self :: $lfc ); | |
echo nl2br( $this->logtext ); | |
} | |
if ( $wrote ) { | |
$this->logtext = ''; | |
} | |
} | |
/** | |
* clear the logfile | |
* | |
* @since 0.3 | |
* @access public | |
* @param bool $archivate | |
* @return bool | |
*/ | |
public function clear_log( $archivate = TRUE ) { | |
$fp = NULL; | |
//rename the current file | |
if ( $archivate | |
&& | |
! rename( | |
self :: $directory . '/' . $this->filename, | |
self :: $directory . '/' . $this->context . date ( '-Y-m-d-H-i-s' ) . $this->extension | |
) | |
) { | |
//okay, try to rename it manually | |
$cache = file_get_contents( self :: $directory . '/' . $this->filename ); | |
$wrote = FALSE; | |
if ( FALSE !== $cache ) { | |
$cfp = @fopen( self :: $directory . '/' . $this->context . date ('-Y-m-d-H-i') . $this->extension, 'w' ); | |
$wrote = @fwrite( $cfp, $cache ); | |
@fclose( $cfp ); | |
} | |
//set filesize to 0 bytes only if there content is saved | |
if ( FALSE !== $wrote ) { | |
$fp = @fopen ( self :: $directory . '/' . $this->filename, 'w' ); | |
} | |
} | |
else { | |
//set filesize to 0 bytes | |
$fp = @fopen( self :: $directory . '/' . $this->filename, 'w' ); | |
} | |
if ( is_resource( $fp ) ) { | |
@fclose( $fp ); | |
return TRUE; | |
} | |
return FALSE; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include './class-Logwriter.php'; | |
/** | |
* setup directory and the environment | |
* | |
* on live systems you needn't call Logwriter::set_live() | |
* by default, the class will not generate any output even if logging fails | |
*/ | |
Logwriter :: set_directory( './log' ); | |
#optional (default is TRUE) | |
Logwriter :: set_live( FALSE ); | |
#optional (default is CRLF) | |
Logwriter :: set_lf ( "\r\n" ); | |
#log something in error.txt | |
$log = new Logwriter( 'error' ); | |
$log->addln( 'Hello World' ); | |
$log->write(); | |
#log something in sql.log | |
$log = new Logwriter( 'sql', '.log' ); | |
#limit filezize of 'sql.log' to 1kib | |
$log->set_max_filesize( pow( 2, 5 ) ); | |
$log->addln( 'SQL-Error: ...' ); | |
$log->write(); | |
#clear a log without archivate the content | |
#$log->clear_log( FALSE ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment