Object exception when using print_r () variables in user logger

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.

+4
source share
5 answers

__toString() is called when an object is passed to a string. You can try something like

 $objectString = method_exists($object, '__toString') ? (string) $object : print_r($object, true); 

Use is_object() to find out if the value is an object or not.

For all

 $string = !is_object($value) || method_exists($value, '__toString') ? (string) $value : print_r($value, true); 
+2
source

You can wrap print_r with your own function, which checks whether the provided data includes any objects using the is_object() function. Similarly, you can use is_a() if you want to exclude only certain classes of objects.

+1
source

as an html solution you can use:

 <pre><?=print_r(log_content);?></pre> 

to better display the log file. I show the log files in this way.

0
source
 if (is_object( $foo )) { print_r( $foo->__toString() ); } else { print_r( $foo ); } 
0
source

If you can change the Logger class, you can check whether the befpre class will be printed by you:

 if(!($var instanceof HeavyMVCObject)){ print_r($var); } 
0
source

All Articles