Last active
August 10, 2016 22:24
-
-
Save NobleUplift/48e0e5e333242a83bbc21830ccc6c977 to your computer and use it in GitHub Desktop.
Sorts Apache HTTP logs for multiple servers behind a load balancer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Created on 2016 April 6th @ 05:03 PM | |
if (count($argv) > 1) { | |
$filename = $argv[1]; | |
} else { | |
error_log('No filename provided.'); | |
exit(); | |
} | |
$file = fopen($filename, 'r'); | |
$lines = array(); | |
while (($line = fgets($file)) !== false) { | |
$begin = strpos($line, ':['); | |
$end = strpos($line, ']', $begin + 2); | |
$date = substr($line, $begin + 2, ($end - $begin - 2)); | |
/* @var $dt \DateTime */ | |
$dt = \DateTime::createFromFormat('D M d H:i:s.u Y', $date); | |
$iso8601 = $dt->format('Y-m-d H:i:s'); | |
$lines[$iso8601] = $line; | |
} | |
ksort($lines); | |
foreach ($lines as $date => $line) { | |
echo $line; | |
} | |
fclose($file); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment