Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对日志消息进行排序_Php_File_Magento - Fatal编程技术网

Php 对日志消息进行排序

Php 对日志消息进行排序,php,file,magento,Php,File,Magento,大家好,我正在尝试从文件中对日志消息进行排序。 我的日志文件如下所示 我想得到这样的东西 ['debug'] => [ ['logs'] => [ ['log message'] => [ '2016-04-10', '2016-04-10', '2016-04-10' ], ['log message'] => [ '2

大家好,我正在尝试从文件中对日志消息进行排序。 我的日志文件如下所示 我想得到这样的东西

['debug'] => [
    ['logs'] => [
        ['log message'] => [
            '2016-04-10',
            '2016-04-10',
            '2016-04-10'
        ],
        ['log message'] => [
            '2016-04-10',
            '2016-04-10',
            '2016-04-10'
        ],
        ...
    ]
],
['Crit'] => [
    ['logs'] => [
        ['log message'] => [
            '2016-04-10',
            '2016-04-10',
            '2016-04-10'
        ],
        ['log message'] => [
            '2016-04-10',
            '2016-04-10',
            '2016-04-10'
        ],
        ...
    ]
]
这是我的代码,但它不能正常工作,它有一些行

$file = fopen(self::DIRECTORY.$filename, 'rb');
while(($line = fgets($file)) !== false) {
    if($this->lineHasDate($line)) {
        $logDate = $this->getLogDate($line);
        $logType = $this->getTypeOfLog($line);
        $content .= $line;
        // $content ='';
    }
    while(!$this->lineHasDate($line) && ($line = fgets($file)) !== false) {
        // echo $line;
        $content .= $line;
    }
    $this->logs[$logType]['logs'][] = $content;
    $content = '';
}
fclose($file);

指定的代码(在循环文件中循环文件)有点太复杂,更容易出错

像这样试试,你的例子对我很有用。阅读和调试更简单、更直接:

$file = fopen (self::DIRECTORY.$filename, 'rb');
while (($line = fgets($file)) !== false)
{
    if ($this->lineHasDate($line))
    {
        $logDate = $this->getLogDate($line);
        $logType = $this->getTypeOfLog($line);

        $this->logs[$logType]['logs'][] = $line;
        $currentLog = &$this->logs[$logType]['logs'][count($this->logs[$logType]['logs']) - 1];
    }
    else
    {
        if (!isset($currentLog)) // In case of trash lines before logs begin
            continue;
        else
            $currentLog .= $line;
    }
}
fclose($file);

另外,测试当您输入省略的行时,
$this->lineHasDate($line)
是否返回
true

指定的代码(在循环文件中循环文件)有点太复杂,更容易出错

像这样试试,你的例子对我很有用。阅读和调试更简单、更直接:

$file = fopen (self::DIRECTORY.$filename, 'rb');
while (($line = fgets($file)) !== false)
{
    if ($this->lineHasDate($line))
    {
        $logDate = $this->getLogDate($line);
        $logType = $this->getTypeOfLog($line);

        $this->logs[$logType]['logs'][] = $line;
        $currentLog = &$this->logs[$logType]['logs'][count($this->logs[$logType]['logs']) - 1];
    }
    else
    {
        if (!isset($currentLog)) // In case of trash lines before logs begin
            continue;
        else
            $currentLog .= $line;
    }
}
fclose($file);

另外,测试当您输入省略的行时,
$this->lineHasDate($line)
是否返回
true

如果不知道
lineHasDate
的外观以及省略了哪些行,就很难提出任何建议。
lineHasDate
检查文件中的当前行是否包含日期如果为真,则检查其新日志消息。例如,我的代码是这一行
2016-09-19T15:16:27+00:01临界值(2):无效的模板文件:frontend/base/default/template/configurableswatches/catalog/media/js.phtml
如果不知道
lineHasDate
的外观以及省略了哪些行,则很难提出任何建议。
lineHasDate
检查文件中的当前行是否包含日期如果为真,则其新日志消息。例如,我的代码是这样的:在每次换行之前,这一行
2016-09-19T15:16:27+00:01 CRIT(2):无效的模板文件:frontend/base/default/template/configurableswatches/catalog/media/js.phtml
。如果它包含日期,则这是一个新的日志条目,因此将向相应的
$logType
'logs'
数组中添加一个值为
$line
的新元素。然后保存对该字符串的引用,以便在不包含日期的情况下将下一行追加到该字符串。非常感谢。它对大文件有效吗?我是说超过1gb?如果要确定数组中的日志消息是否与文件中的日志消息相同,则函数的外观如何?如果为true,则为该日志添加日期。日志消息应该是数组中的新索引,并且发生日期应该在此数组中。我已经编辑了这个问题。在每次循环之前,你们都要换一行。如果它包含日期,则这是一个新的日志条目,因此将向相应的
$logType
'logs'
数组中添加一个值为
$line
的新元素。然后保存对该字符串的引用,以便在不包含日期的情况下将下一行追加到该字符串。非常感谢。它对大文件有效吗?我是说超过1gb?如果要确定数组中的日志消息是否与文件中的日志消息相同,则函数的外观如何?如果为true,则为该日志添加日期。日志消息应该是数组中的新索引,并且发生日期应该在此数组中。我编辑了这个问题。