Opencart错误日志显示唯一错误

时间:2013-01-10 12:25:04

标签: php opencart

我如何修改file_get_contents以便它只返回1000行重复项中的唯一错误

$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

完整代码:在Jay的帮助下

$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 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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];
    }
}