Skip to content

Instantly share code, notes, and snippets.

@UziTech
Last active August 29, 2015 14:05
Show Gist options
  • Save UziTech/ad4eec4c294aed72f34a to your computer and use it in GitHub Desktop.
Save UziTech/ad4eec4c294aed72f34a to your computer and use it in GitHub Desktop.
Show logs from a file that matches a regular expression
<?php
/**
* DWTFYW License
*
* Author: Tony Brix, http://tonybrix.info
*
* Show logs from a file that matches a regular expression. Matches multiline records.
* @param string $file The file to search
* @param string $search [optional] A string or regular expression to match; default = ""
* @param boolean $isregexp [optional] A regular expression to match; default = false
* @param datetime $startdate [optional] Only search after this date
* @param datetime $enddate [optional] Only search before this date
* @param string $recordBeginning [optional] A regular expression for the beginning of a record in the file. Default
* @return array
*/
function searchLog($file, $search = "", $isregexp = false, $startdate = null, $enddate = null, $recordBeginning = null) {
//turn off error reporting so we aren't writing errors to the file.
error_reporting(0);
//set recordBeginning
if ($recordBeginning === null) {
$recordBeginning = "/^\[(?P<datetime>\d\d-\w\w\w-\d\d\d\d \d\d:\d\d:\d\d) (?P<timezone>" . preg_quote(date_default_timezone_get(), "/") . ")\]/";
}
//check startdate and enddate
if ($startdate !== null && is_string($startdate)) {
$startdate = strtotime($startdate);
}
if ($enddate !== null && is_string($enddate)) {
$enddate = strtotime($enddate);
}
//get pattern to match
if ($isregexp) {
$pattern = "/^{$search}$/s";
} else {
$pattern = "/^.*" . preg_quote($search, "/") . ".*$/s";
}
$matchedRecords = array();
//$notMatchedRecords = array();
$recordNumber = 0;
$lastRecordTime = null;
$lastRecordTimezone = null;
$record = $record;
$handle = fopen($file, "r");
if ($handle) {
while (($buffer = fgets($handle)) !== false) {
//check if this is the beginning of a new record.
$matches = null;
if (preg_match($recordBeginning, $buffer, $matches)) {
$recordNumber++;
//search $record
if ($record !== null) {
if (($startdate === null || $lastRecordTime > $startdate) && ($enddate === null || $lastRecordTime < $enddate) && preg_match($pattern, $record)) {
$matchedRecords[] = array(
"number" => $recordNumber,
"time" => $lastRecordTime,
"timezone" => $lastRecordTimezone,
"record" => $record,
);
} /* else {
$notMatchedRecords[] = array(
"number" => $recordNumber,
"time" => $lastRecordTime,
"timezone" => $lastRecordTimezone,
"record" => $record,
);
} */
}
$lastRecordTime = strtotime($matches["datetime"]);
$lastRecordTimezone = $matches["timezone"];
$record = $buffer;
} else {
$record .= $buffer;
}
}
//search last record
if (($startdate === null || $lastRecordTime > $startdate) && ($enddate === null || $lastRecordTime < $enddate) && preg_match($pattern, $record)) {
$matchedRecords[] = array(
"number" => $recordNumber,
"time" => $lastRecordTime,
"timezone" => $lastRecordTimezone,
"record" => $record,
);
} /* else {
$notMatchedRecords[] = array(
"number" => $recordNumber,
"time" => $lastRecordTime,
"timezone" => $lastRecordTimezone,
"record" => $record,
);
} */
fclose($handle);
}
return array(
"total" => $recordNumber,
//"pattern" => $pattern,
"records" => $matchedRecords,
//"notmatch" => $notMatchedRecords,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment