<?php
class SQLReactorLogger {
public $file = 'log/log.txt';
public $level;
protected $_config;
final public function __construct(){
$this->getConfiguration();
}
public function debug($msg){
if( $this->level == 'debug' ){
$this->setMsg( strftime( '%Y-%m-%d %H:%M:%S' ) . " DEBUG: " . $msg . "\n" );
}
}
public function clear(){
if( filesize( $this->file ) / 1024 > $this->_config[ 'max_size' ] ){
file_put_contents( $this->file, '' );
}
}
public function error($msg){
$this->setMsg( strftime( '%Y-%m-%d %H:%M:%S' ) . " ERROR: " . $msg . "\n" );
}
private function setMsg( $msg ){
if( !$msg ) return;
$this->clear();
$sucess = file_put_contents( $this->file, $msg, FILE_APPEND );
if( !$sucess ) throw new Exception( "Can't write in the log file!" );
}
private function getConfiguration(){
$this->_config = SQLReactor::$_config['logger'];
$this->level = strtolower( $this->_config['level'] );
$this->basePath = realpath( dirname( __FILE__ ) . '/../' ) . '/';
$this->file = $this->basePath . $this->file;
}
}
?>