我有Logger-Class,它記錄了所有內(nèi)容.將使用print_r將對(duì)象記錄到人類可讀狀態(tài).我的問(wèn)題是我有一個(gè)很大的MVC-Object.每次發(fā)生異常或錯(cuò)誤時(shí),MVC對(duì)象也將打印在Log by print_r中.這會(huì)導(dǎo)致非常長(zhǎng)的Logfile對(duì)讀取起來(lái)不太友好.
我試圖將__toString()方法設(shè)置為我的MVC-Class,但這不起作用.我也在Log中獲得了完整的MVC-Object. MVC是一個(gè)Singleton,并在每個(gè)Object上引用.因此,在進(jìn)入print_r之前簡(jiǎn)單地排除Object并不容易.
有沒(méi)有辦法從print_r中排除對(duì)象?
我的方法:
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錯(cuò)誤 – 方法:
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);}
設(shè)置錯(cuò)誤和異常處理程序:
//Set Error and Exception Handler set_error_handler(array(new LOG(), 'errorHandler')); set_exception_handler(array(new LOG(), 'exceptionHandler'));
謝謝
一些測(cè)試:
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; }
不工作.也許RegEx失敗了嗎?
解:
這是一個(gè)設(shè)計(jì)缺陷. errorHandler是記錄發(fā)生錯(cuò)誤的位置上使用的所有對(duì)象.所以在index.php中有以下代碼:
$mvc = Megaira_MVC::getInstance();
因此,這種代碼的平衡通過(guò)LOG-Class中的errorHandler生成了帶有print_r的Var $mvc的日志記錄.
結(jié)論對(duì)我來(lái)說(shuō):不要在大單例對(duì)象上使用變量,或者如果不再需要Var,則使用unset().
解決方法:
當(dāng)對(duì)象轉(zhuǎn)換為字符串時(shí),將調(diào)用__toString().你可以試試像
$objectString = method_exists($object, '__toString') ? (string) $object : print_r($object, true);
如果值是對(duì)象,則使用is_object()查找.
完全沒(méi)有
$string = !is_object($value) || method_exists($value, '__toString') ? (string) $value : print_r($value, true);
來(lái)源:https://www.icode9.com/content-1-284551.html
聯(lián)系客服