Skip to content

Instantly share code, notes, and snippets.

@NobleUplift
Last active August 10, 2016 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NobleUplift/48e0e5e333242a83bbc21830ccc6c977 to your computer and use it in GitHub Desktop.
Save NobleUplift/48e0e5e333242a83bbc21830ccc6c977 to your computer and use it in GitHub Desktop.
Sorts Apache HTTP logs for multiple servers behind a load balancer
<?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