【问题标题】:Reading errors and file names from a log.txt file从 log.txt 文件中读取错误和文件名
【发布时间】:2012-12-09 08:40:01
【问题描述】:

继续Getting the file name from a text file after string matching - PHP 我有一个 log.txt 格式的文件:

================================================
Header of entry with date and time
================================================
Loaded options from XML file: '/path/to/the/file/some_file.xml
extendedPrintPDF started
Postfix '4.4' was append from file 'some-other_file.xml' for file: /path/to/the/file/some-other_file.indd
printPDF started
PDF Export Preset: Some preset
PDF file created: '/path/to/the/file/some_filey_namex_lo_4.4.pdf'.
File filename_lo-metadata.xml removed
postprocessingDocument started
INDD file removed: /path/to/the/file/some_file1_name_lo.indd
Error opening document: /path/to/the/file/some_file_name_lo.indd: Error: Either the file does not exist, you do not have permission, or the file may be in use by another application; line: 190
================================================
Header of entry with date and time
================================================
Loaded options from XML file: '/path/to/she/file/some_options.xml
extendedPrintPDF started
extendedPrintPDF: Error: Unsaved documents have no full name.; line: 332

我想为日志条目捕获单词“错误”(如果存在),然后检测错误消息(如“未保存的文档没有全名”)或只是“打开文档时出错”,并获取所有且只有正在创建的 pdf 文件。

我是新手,不知道该怎么做。

我想我已经提供了完整的信息,而我在之前的帖子中没有做到这一点。

改变: 我还想从 /path/to/the/file/filename.something 中获取“the”。 我不是在寻找一个确切的答案(即使它会很棒)。但是,我们将不胜感激。

注意: 有没有办法在不使用 RegEx 的情况下做到这一点(我是新手,而且我一点也不擅长 RegEx) 谢谢

【问题讨论】:

  • 精通 RegEx 的最佳方法是坐在在线 RegEx 测试仪上,然后弄清楚这样的事情 :-)
  • @Bart:明白了。谢谢。 :)

标签: php wordsearch


【解决方案1】:

正则表达式

如何获取文件名:

#(/.*/\w+\.\w{3})#

如何获取错误:

/Error: (.*)/m

PHP

<?php

$file_data = __your__file__data__;

preg_match_all("#(/.*/\w+\.\w{3})#", $file_data, $filenames);
var_dump($filenames);

preg_match_all("/Error: (.*)/m", $file_data, $errors);
var_dump($errors);

?>

示例输出

array (size=2)
  0 => 
    array (size=3)
      0 => string '/the/path/of/log_options.xml' (length=28)
      1 => string '/path/of/some/filesomething.ind' (length=31)
      2 => string '/the/path/of/log_options.xml' (length=28)
  1 => 
    array (size=3)
      0 => string '/the/path/of/log_options.xml' (length=28)
      1 => string '/path/of/some/filesomething.ind' (length=31)
      2 => string '/the/path/of/log_options.xml' (length=28)

array (size=2)
  0 => 
    array (size=2)
      0 => string 'Error: file doesnt exist or no permissions 
' (length=44)
      1 => string 'Error: Unsaved documents have no full name: line xyz' (length=52)
  1 => 
    array (size=2)
      0 => string 'file doesnt exist or no permissions 
' (length=37)
      1 => string 'Unsaved documents have no full name: line xyz' (length=45)

我会让你弄清楚如何使用这些信息并将其放入你的数据库中。

【讨论】:

  • 任何时候 :-),有时无法避免野兽。
  • 我很抱歉我没有读到你的名字并且在我的评论中搞砸了:(不过非常感谢。
  • 文件名的 RegEx 仅返回 .indd 和 .xml 文件。但是,我只需要 .pdf 文件。如何更改正则表达式以仅捕获扩展名为 .pdf 的文件名?另外,我需要从 /the/path/of/file 部分捕获“路径”。你建议我怎么做?附言你不必给我确切的答案。如果你能指导我就好了。 :)
【解决方案2】:

这真的只匹配你给我们的有限日志 sn-p 的语法,但它应该做一个开始。它逐行遍历日志文件,并尽最大努力跟踪它所在的日期部分。

<?php
$logfile = '/path/to/the/file.log';
$fh = fopen($logfile, 'r') or die("can't open file.");

$r_delim = '/^=*$/';            // matches lines of only =
$r_time  = '/^\[(.*)\] - .*$/'; // matches lines that start with [, captures contents between [ and ]
$r_error = '/ Error: /';        // matches lines that contain the string ' Error: '

$skip_delim = FALSE;
$last_timestamp = 'no stamp';
$matches = array();

while($line = fgets($fh)) {
        if( preg_match($r_delim, $line) && ! $skip_delim) {
                $line = fgets($fh);
                preg_match($r_time, $line, $matches);
                $last_timestamp = $matches[1];
                $skip_delim = TRUE;
        } else if( preg_match($r_delim, $line) && $skip_delim ) {
                $skip_delim = FALSE;
        } else if( preg_match($r_error, $line) ) {
                printf('%s :: %s', $last_timestamp, $line);
        }
}

输出:

Jan 25 2012 11:26:03 :: Error opening document: /the/path/to/file/some_file_name_lo.indd: Error: Either the file does not exist, you do not have permission, or the file may be in use by another application; line: 190
Feb 24 2012 15:57:43 :: extendedPrintPDF: Error: Unsaved documents have no full name.; line: 332
Feb 24 2012 15:57:43 :: extendedPrintPDF: Error: Unsaved documents have no full name.; line: 332

【讨论】:

  • 太棒了。但是,这不会捕获带有 .pdf 扩展名的文件名,对吧?!我想我可以尝试 phpisuber01 的答案来完成这项工作。也会试试这个。非常感谢。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多