0

Continuing from Getting the file name from a text file after string matching - PHP I have a log.txt file in the format:

================================================
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

I want to catch the word "Error" (if present) for a log entry and then detect the error message (like "Unsaved documents have no full name") or just "Error opening document" and also get the names of all and ONLY the pdf files being created.

I'm a newbie and do not really know how I should go about doing this.

I think I have provided complete information, which I failed to do in my previous post.

CHANGE: I would also like to grab 'the' from /path/to/the/file/filename.something. I am not looking for an EXACT answer (even though it would be great). But, some help or guidance would be appreciated.

NOTE: Is there a way to do it WITHOUT using RegEx (I'm a newbie and also, I am not at all good at RegEx) Thanks

Community
  • 1
  • 1

2 Answers2

1

RegEx

How to get the file name:

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

How to get the Error:

/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);

?>

Example Output

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)

I'll let you figure out how to use the information and put it into your database.

Bart
  • 19,692
  • 7
  • 68
  • 77
phpisuber01
  • 7,585
  • 3
  • 22
  • 26
  • Anytime :-), sometimes there is no avoiding the beast. – phpisuber01 Dec 21 '12 at 19:22
  • And I'm sorry I didn't read your name and goofed up in my comment :( Thanks a bunch though. – Watchful Protector Dec 21 '12 at 19:31
  • The RegEx for the file name returns only the .indd and .xml files. However, I need the .pdf files ONLY. How can I change the RegEx to catch only the file name with .pdf extension? Also, I need to catch 'path' from /the/path/of/file part. How do you suggest I go about doing that? P.S. You don't have to give me the EXACT answers. It would be okay if you can just guide me. :) – Watchful Protector Dec 26 '12 at 17:23
0

This really only matches the syntax of the limited log snippet you gave us, but it should do for a start. It runs through the log file line-by-line and does its best to keep track of which dated section it's in.

<?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);
        }
}

Output:

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
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • Great. But, this would not catch the file names with .pdf extensions right?! I guess I can try phpisuber01's answer to make that work. Will try this as well. Many thanks. :) – Watchful Protector Dec 21 '12 at 19:39