Apache log generation
We can use Regular expression to match logs like cpanel and Directadmin make. This will read the line form a log file and returns a array of data in each log line.
This function will match each data like ip, user, date, time etc from each log line and return an array.
function format_log_line($line)
{
preg_match(”/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \”(\S+) (.*?) (\S+)\” (\S+) (\S+) (\”.*?\”) (\”.*?\”)$/”, $line, $matches); // pattern to format the line
return $matches;
}
The array returned form the above function can be used as below and printed in a tabular format
function format_line($line)
{
$logs = format_log_line($line);
if (isset($logs[0]))
{
$formated_log = array();
$formated_log[’ip’] = $logs[1];
$formated_log[’identity’] = $logs[2];
$formated_log[’user’] = $logs[2];
$formated_log[’date’] = $logs[4];
$formated_log[’time’] = $logs[5];
$formated_log[’timezone’] = $logs[6];
$formated_log[’method’] = $logs[7];
$formated_log[’path’] = $logs[8];
$formated_log[’protocal’] = $logs[9];
$formated_log[’status’] = $logs[10];
$formated_log[’bytes’] = $logs[11];
$formated_log[’referer’] = $logs[12];
$formated_log[’agent’] = $logs[13];
return $formated_log;
}
else
{
echo ‘there was an error with that line’;
return false;
}
}
