I have a Logger-Class that logs everything. Objects will be registered using print_r according to the person. My problem is that I have a large MVC object. Each time an exception or error occurs, the MVC object will also be printed in the log by print_r. This results in a very long Logfile, which is not very read friendly.
I tried to set the __toString() method for my MVC class, but this does not work. I also get the full MVC object in the log. MVC is Singleton and refers to each object. Thus, to simply exclude an object before it fits into print_r, it is not so simple.
Is there a way to exclude objects from print_r?
My methods are:
LOG-Class errorHandler-Method:
public static function errorHandler($errno, $errstr, $errfile, $errline, $vars) { //If @ is set, don't do anything! if(error_reporting() === 0) { return; } //Get StackTrace with low memory usage ;) $e = new Exception(); $stackStr = $e->getTraceAsString(); //Build ErrorMessage for Log $message = 'File: '.$errfile.' - L: '.$errline."\n". 'Code: '.$errno."\n". 'Message: '.$errstr."\n". 'Vars: '.print_r($vars, true)."\n". 'Stacktrace: '.$stackStr; self::error($message); }
LOG-Class exceptionHandler-Method:
public static function exceptionHandler(Exception $e) { $message = get_class($e).': '.$e->getMessage()."\n". 'File: '.$e->getFile().' - L: '.$e->getLine()."\n". 'Code: '.$e->getCode()."\n". 'Message: '.$e->getMessage()."\n". 'Stacktrace: '.$e->getTraceAsString(); self::error($message); }
LOG-Class Method Error:
public static function error($data, $file='system.log') { $config = Megaira_PropertyConfiguration::getInstance(); switch($config->get('LOG_MODE')) { case'DEEPDEBUG': case'ERROR': case'WARNING': case'INFO': case'DEBUG': self::writeToLog('ERROR', $data, $file); break; } }
LOG-Class writeToLog-Method:
private static function writeToLog($mode='', $text='', $file=''){ if(!is_string($text) && !is_numeric($text)) { $text = print_r($text, true); } $config = Megaira_PropertyConfiguration::getInstance(); if(!$config->get('LOGGINGACTIVE')) { return; } self::writeLineToFile($mode, $text, $file); }
Install an error and exception handler:
//Set Error and Exception Handler set_error_handler(array(new LOG(), 'errorHandler')); set_exception_handler(array(new LOG(), 'exceptionHandler'));
thanks
Some testing:
public static function print_r_filtered($object, $ret=false) { $filtered = array( 'Megaira_MVC' ); $text = print_r($object, true); foreach($filtered as $filter) { $search = '#('.$filter.'\sObject)\n(\s+)\).*?\n\2\)\n#s'; $replace = "$1"; $text = preg_replace($search, $replace, $text); } if($ret) return $text; echo $text; }
Does not work. Maybe RegEx is not working?
Decision:
This was a design flaw. ErrorHandler registers all objects that are used at the location of the error. So in index.php there is the following code:
$mvc = Megaira_MVC::getInstance();
So, this world of code caused Var $mvc to register with print_r via errorHandler in the LOG-Class.
Conclusion for me: do not use variables on large Singleton-Objects or use unset () if Var is no longer needed.