3

我怎么能修改它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>
4

2 回答 2

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;
于 2013-01-10T16:57:57.990 回答
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];
    }
}
于 2013-01-10T12:28:26.793 回答