如何解决在自定义记录器中使用print_r变量时不包括对象
|| 我有Logger-Class,它正在记录所有内容。对象将使用print_r记录到人类可读的状态。我的问题是我有一个很大的MVC对象。每当发生异常或错误时,MVC对象也将由print_r打印在日志中。这将导致一个非常长的Logfile,它实际上并不易于读取。 我试图为我的MVC-Class设置一个“ 0”方法,但是这种方法不起作用。我还在Log中获得了完整的MVC对象。 MVC是一个Singleton,在每个对象上都被引用。因此,在对象进入print_r之前简单地排除它并不是那么容易。 有什么办法可以从print_r中排除对象? 我的方法: LOG类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类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类错误-方法:
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类的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);
}
设置错误和异常处理程序:
//Set Error and Exception Handler
set_error_handler(array(new LOG(),\'errorHandler\'));
set_exception_handler(array(new LOG(),\'exceptionHandler\'));
谢谢
一些测试:
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失败了?
解:
这是设计缺陷。 errorHandler正在记录发生错误的位置上使用的所有对象。因此在index.php中是以下代码:
$mvc = Megaira_MVC::getInstance();
因此,这种和平的代码通过LOG-Class中的errorHandler使用print_r记录了VarVar8ѭ。
对我的结论:不要在大型Singleton对象上使用变量,如果不再需要Var,则不要使用unset()。
解决方法
当对象强制转换为字符串时,将调用“ 0”。您可以尝试类似
$objectString = method_exists($object,\'__toString\')
? (string) $object
: print_r($object,true);
使用is_object()
找出值是否是对象。
完全没有
$string = !is_object($value) || method_exists($value,\'__toString\')
? (string) $value
: print_r($value,true);
, 您可以使用is_object()
函数用自己的函数包装print_r,该函数检查所提供的数据是否包含任何对象。同样,如果只想排除某些类的对象,则可以使用ѭ14。
, 作为html解决方案,您可以使用:
<pre><?=print_r(log_content);?></pre>
以便更好地显示您的日志文件。我以这种方式显示日志文件。
, if (is_object( $foo ))
{
print_r( $foo->__toString() );
} else {
print_r( $foo );
}
, 如果可以更改Logger类,则可以在打印它之前检查该类:
if(!($var instanceof HeavyMVCObject)){
print_r($var);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。