2013-01-10 56 views
3

我怎麼能修改file_get_contents,使其只從1000返回重複Opencart的錯誤日誌顯示獨特的錯誤

行的唯一錯誤
$this->data['log'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null); 

<textarea wrap="off" style="width: 98%; height: 300px; padding: 5px; border: 1px solid #CCCCCC; background: #FFFFFF; overflow: scroll;"><?php echo $log; ?></textarea> 

錯誤:

2013-01-10 0:26:45 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 0:27:02 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 0:27:04 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 0:29:25 - PHP Notice: Undefined variable: thumb in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 23 
2013-01-10 0:29:25 - PHP Notice: Undefined variable: description in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 23 
2013-01-10 0:29:25 - PHP Notice: Undefined variable: categories in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 34 
2013-01-10 0:29:25 - PHP Notice: Undefined variable: categories in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 98 
2013-01-10 2:46:25 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 6:09:58 - PHP Notice: Undefined variable: thumb in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 23 
2013-01-10 6:09:58 - PHP Notice: Undefined variable: description in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 23 
2013-01-10 6:09:58 - PHP Notice: Undefined variable: categories in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 34 
2013-01-10 6:09:58 - PHP Notice: Undefined variable: categories in /var/www/example/public/catalog/view/theme/example/template/product/manufacturer_info.tpl on line 98 
2013-01-10 9:54:45 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 10:43:53 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 12:03:27 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 12:03:37 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 12:03:44 - PHP Notice: Undefined index: name in /var/www/example/public/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 159 
2013-01-10 13:46:37 - PHP Warning: imagecreatetruecolor() [<a href='function.imagecreatetruecolor'>function.imagecreatetruecolor</a>]: Invalid image dimensions in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 241 
2013-01-10 13:46:37 - PHP Warning: imagecolorallocate() expects parameter 1 to be resource, boolean given in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 249 
2013-01-10 13:46:37 - PHP Warning: imagefilledrectangle() expects parameter 1 to be resource, boolean given in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 252 
2013-01-10 13:46:37 - PHP Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 254 
2013-01-10 13:46:37 - PHP Warning: imagecreatetruecolor() [<a href='function.imagecreatetruecolor'>function.imagecreatetruecolor</a>]: Invalid image dimensions in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 241 
2013-01-10 13:46:37 - PHP Warning: imagecolorallocate() expects parameter 1 to be resource, boolean given in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 249 
2013-01-10 13:46:37 - PHP Warning: imagefilledrectangle() expects parameter 1 to be resource, boolean given in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 252 
2013-01-10 13:46:37 - PHP Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /var/www/example/public/vqmod/vqcache/vq2-system_library_image.php on line 254 

完整代碼:與幫助周杰倫

$this->data['log'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null); 

替換:

$input = file_get_contents($file, FILE_USE_INCLUDE_PATH, null); 
$temp = preg_replace('~\d{4}-\d\d-\d\d \d\d?:\d\d:\d\d - ~', '', $input); 
$this->data['log'] = implode(PHP_EOL, array_unique(explode(PHP_EOL, $temp))); 

vqmod

<file name="admin/controller/tool/error_log.php"> 
    <operation> 
     <search position="before"><![CDATA[ 
     $this->data['clear'] = $this->url->link('tool/error_log/clear', 'token=' . $this->session->data['token'], 'SSL'); 
     ]]></search> 
     <add><![CDATA[ 
     $this->data['action'] = $this->url->link('tool/error_log', 'token=' . $this->session->data['token'], 'SSL'); 
     ]]></add> 
    </operation> 
    <operation> 
     <search position="after"><![CDATA[ 
     $this->data['log'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null); 
     ]]></search> 
     <add><![CDATA[ 
     if (!isset($this->request->post['unique'])){ 
      $this->data['unique'] = ''; 
     } else { 
      $this->data['unique'] = $this->request->post['unique']; 

      $temp = preg_replace('~\d{4}-\d\d-\d\d \d\d?:\d\d:\d\d - ~', '', $this->data['log']); 
      $this->data['log'] = implode(PHP_EOL, array_unique(explode(PHP_EOL, $temp))); 
     } 
     ]]></add> 
    </operation> 
</file> 
<file name="admin/view/template/tool/error_log.tpl"> 
    <operation> 
     <search position="after"><![CDATA[ 
     $button_clear 
     ]]></search> 
     <add><![CDATA[ 
     <form action="<?php echo $action; ?>" method="post" class="buttons"> 
      <label> 
      <input type="checkbox" name="unique" onchange="this.form.submit();" value="1" <?php if($unique){ echo ' checked="checked"'; } ?> /> Unique 
      </label> 
     </form> 
     ]]></add> 
    </operation> 
</file> 

回答

2

有這樣做的一個非常簡單的方法。設置你的錯誤日誌文本爲$input,然後使用

$temp = preg_replace('~\d{4}-\d\d-\d\d \d\d?:\d\d:\d\d - ~', '', $input); 
$output = implode(PHP_EOL, array_unique(explode(PHP_EOL, $temp))); 

echo $output; 
+0

+1非常印象如此之短。 –

+0

周杰倫,你有(或知道我在哪裏可以找到它)和VQMod這個修復文件?我無法在谷歌上找到它... – Mathlight

+0

@Mathlight - 這實際上是你需要一個vQmod的東西。你只需要一個php文件,並用我看到的日誌文件 –

1

使用此代碼行只拿到唯一的錯誤線

$errors = array(); 
foreach(file($file)) as $error_line) { 
    $regex = '/^([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2})\s-\s(.+)$/'; 
    preg_match($regex, $error_line, $match); 

    $hash = md5($match[2]); 

    if (!isset($errors[$hash])) { 
     $errors[$hash] = array(
      'error_line' => $match[2], 
      'times' = array(
       $match[1], 
      ), 
     ); 
    } else { 
     $errors[$hash]['times'][] = $match[1]; 
    } 
} 
+1

我不認爲它會去工作,除非你剝去每一行的時間戳。 –

+0

這就是我改變了我的答案 – GreenRover

+1

正則表達式會拋出一個錯誤,因爲它沒有任何分隔符 –